I've just read Can awk patterns match multiple lines?, whose accepted solution is a script that print the line after first half
.
how
second half #1
now
first half
second half #2
brown
second half #3
cow
/second half/ {
if(lastLine == "first half") {
print
}
}
{ lastLine = $0 }
This gives second half #2
.
I can't understand why { lastLine = $0 }
has to go after /second half/ {...}/
. I tried interchanging them, and I got nothing.
{ lastLine = $0 }
/second half/ {
if(lastLine == "first half") {
print
}
}
I tried reading man awk
, but it doesn't cover state machines. Searching "awk state machine" gives only the linked SO question.