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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop over repeating blocks 1

Status
Not open for further replies.

FedoEx

Technical User
Oct 7, 2008
49
US
Example data.in hundreds of blocks that look something like that
Code:
#S 1
a  b c d
11 2 3 0
31 5 6 1
72 8 9 2
end block one
#S 2
a  f g h
24 3 4 0
62 7 8 1
52 6 8 2
45 7 9 3
87 4 1 4
end block two
#S 3
d  g k j
11 4 5 0 
33 1 3 1
43 4 6 2
end block  three
The numbers of lines with numbers is always odd.
So if I need to extract the middle row first column of block one the code
Code:
 awk '/#S 1/,/end/{ 
if($0!~/[a-z]/) vec[++nr,1]= sprintf("%i\n", $1) } 
END{print vec[int((nr+1)/2),1]}' data.in
will return 31. If I change #S 1 to #S 2 will give me the result 52.
Is it possible to automate that procedure.
The desired output would be.
Code:
1 31
2 52
3 33
The only way I can think of doing this is via for loop in bash that would call the awk script above.
Is there another way of doing this entirely in awk.
Probably I need to find another way of extracting the desired element without the use of
END{...} block. Then I can define that as a function in awk and call that function for each occurrence of the regular expression #S.
Thanks

 
Thanks. That helped me a lot.
Before reading your answer I thought I had some awk knowledge.
I will get back to you with some questions on that example.
Thanks again!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top