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!

Matching block of text (newbie question) 1

Status
Not open for further replies.

unterkirschner

Programmer
Nov 20, 2008
2
IT
I am struggling to "think" like AWK but I don't seem to get it. I have a file made of blocks like these

Head;aasdad;grgsrg;asdawasd;12345;dsdad
stuff;otherstuff
stuff2;otherstuff2
stuffx;otherstuffx;
Head;asggdvxaa;estuff;otherstuff
stuff2;otherstuff2
Head;asggdvxaa;e
...and so on. I must output the blocks where there's the 12345 in the 5th field, and all the stuff UNTIL the next "Head". I tried with
/^12345/,/^Head/
But this apparently matches a single line and not a block. I'm delving in the conditional structures but I don't understand how to tell awk to "print everything until you match the NEXT "Head". I feel there must be an elegant solution but I cannot grasp it.

Thank you in advance for your insight.
 

Try these:
Code:
awk -F';' '
$1 == "Head" {sw=0}
$1 == "Head" && $5 == "12345" {sw=1}
sw == 1 {print $0}
' MyFile.dat
## Or ...
awk '
/^Head/ {sw=0}
/^Head/&&/12345/ {sw=1}
sw == 1 {print $0}
' MyFile.dat
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Thanks. I can't believe I coulnd't figure that out on my own. Gotta learn awk-thinking...

[medal]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top