Let's break out that awk
script into a file by itself and call that file script.awk
:
#!/usr/bin/awk -f
BEGIN { C = 1; s = "pants" }
{ a[NR] = $0 }
$0 ~ s { f[NR]++ }
END {
for (j = 1; j <= NR; j++)
if (f[j])
for (i = j + C; i <= j + C; i++)
print a[i]
}
I added the initialization of the variables s
and C
in a BEGIN
block, just for my own convenience while testing. Your script takes these from the command line.
Add current line last in array a
.
If the line contains the string s
(tested as regular expression), increment array f
at index
corresponding to current line (this is the same as f[NR] = 1
in this case).
At end of input: Loop through all stored lines, and if we've flagged the line in the f
array print the stored lines, starting at the flagged line plus C
and continuing to, uhh, no, just print that single line (bug here?).
Our script will therefore print every line that comes after any line matching the regular expression s
.
Testing it:
$ cat data.in
Hello world!
My pants
are on fire!
No, hold on,
Those are not *my* pants.
Phew!
$ chmod +x script.awk
$ ./script.awk data.in
are on fire!
Phew!
So, in your case (replace "pants" with "NetApp" here), it's virtually identical to (using GNU sed
)
sed -n '/pants/,+1p' data.in | grep -v 'pants'
or
grep -A1 'pants' data.in | grep -E -v 'pants|-'
Personal note: This is why I hate so-called "one-liners". They are ugly, nobody understands them, and they are totally unmaintainable. Please, please, put complex tasks in a scripts, write it cleanly and with comments, and use that as your "one-liner".
EDIT: After some thinking, I realized that thise script probably tries to implement grep -Cnum re
for num = C
and re = s
(to get C
lines of context), but it gets the loop indices wrong in the inner loop (should be i = j - C; i <= j + C
).