I will be talking more about awk, awk vs. perl later, but this time I would like to demonstrate how easy it is to have versatile multi-grep functionality using AWK.
The goal is to print previous or next line that follows/goes before a line consisting of dashes (i.e. comments in C++/Java, text file with headings, etc.).
The text file is
test.txt
before line 1
--------
after line 1
x
y
z
before line 2
--------
after line 2
x
y
z
before line 3
--------
after line 3
x
y
z
grepAfter.awk
/^-+$/{
getline nextLine
print nextLine
}
Command line:
awk -f grepAfter.awk test.txt
Output Listing:
after line 1
after line 2
after line 3
grepBefore.awk
/^-+$/{
print prevLine
}
{
prevLine = $0
}
Command line:
awk -f grepBefore.awk test.txt
Output Listing:
before line 1
before line 2
before line 3
No comments:
Post a Comment