Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using awk to print lines at matching "offsets" in file 1

Status
Not open for further replies.

moria6

MIS
Jul 11, 2000
30
0
0
US
I am trying to print lines from a file in the format

line (a), line (a + (# of lines in file)/2).

Example file shown below (MAC addresses followed by switch port numbers for the curious, pulled from an snmpwalk). First MAC adrs (line 1 in this case) goes with first port (line 9) and so on...


"1e 47 29 00 A0 60 "
"1e 47 29 00 A0 64 "
"1e 47 29 00 A0 AF "
"1e 47 29 00 B1 50 "
"0C 72 0A 27 5F 71 "
"0C 72 0A 27 BC BA "
"2B 07 D4 A2 30 CD "
"2B 07 D4 A2 33 64 "
9
3
7
1
4
2
10
8

I know how to concatenate lines with awk thanks to the fine people on this forum, e.g.


concatenate every 5 lines of input, using a comma separator between fields

awk 'ORS=NR%5?",":"\n"' file


I'm trying to get a handle on how to use awk with NR or NF to print in the required format but I can't quite figure out how to hand awk the (a + (number of lines in file)/2) variable using possibly NF or NR.

Also, have tried writing the file to an array but my awk array skills need some serious work. And placing the two halves (MAC adrs and port) in two separate files and thence two separate arrays definitely stretched those skills - and still didn't work.

I am not tied to an awk solution - awk was just a starting point for me. A sed or perl script would be perfectly acceptable. Learning is good! ;)

Thanks in advance!


maurie
 
Typed, untested:
Code:
awk '{a[NR]=$0}END{for(i=1;i<=NR/2)print a[i]","a[i+NR/2)}' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you, Sir!

Two syntax errors appeared (as you wrote "...typed, untested").

Added close ']' near the end and ",i++" in the for statement

awk '{a[NR]=$0}END{for(i=1;i<=NR/2;i++)print a","a[i+(NR/2)]}' file


Thank you again for your help!!!


maurie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top