Hi,
I have been writing a script for work for some time now and almost have it completed, except for one last step. I use Linux at home, so I am used to using GNU grep to find lines above and below the search string (using -A and -B), but we use Solaris at work and unfortunately it doesn't have GNU grep. I have basically had to learn awk and sed to get around this. What I really need to accomplish is printing one particular string (we'll call it "string1")
or another string ("string2")
and the line above string2 if it is found in the file. I have been using:
Code:
awk '/(^string1)|(^ string2)/{print x; print};{x=$0}/'
...to print the line above the two strings I am searching for so far, which has worked, but unfortunately it also prints the line above string1. I have tried all sorts of different awk commands and played around with the logic, but either it doesn't work and errors out, or I don't get the output I need.
Example file ("NODE" would be "string1" and "description" would be "string2" if using the code I pasted above):
NODE_NAME_1
PORT 1
description 123 blah
PORT 2
PORT 3
PORT 4
description 456 blah blah
PORT 5
NODE_NAME_2
PORT 1
PORT 2
description flibbity jibbity
PORT 3
description 789 blah blah blah
PORT 4
description 101112 blah blah
PORT 5
NODE_NAME_3
PORT 1
PORT 2
PORT 3
PORT 4
PORT 5
I basically need all NODE_NAME lines, plus any PORT lines, but only if they are followed by "description." I want the output to look like this:
NODE_NAME_1
PORT 1
description 123 blah
PORT 4
description 456 blah blah
NODE_NAME_2
PORT 2
description flibbity jibbity
PORT 3
description 789 blah blah blah
PORT 4
description 101112 blah blah
NODE_NAME_3
However, it comes out like this currently:
NODE_NAME_1
PORT 1
description 123 blah
PORT 4
description 456 blah blah
PORT 5
NODE_NAME_2
PORT 2
description flibbity jibbity
PORT 3
description 789 blah blah blah
PORT 4
description 101112 blah blah
PORT 5
NODE_NAME_3
Obviously, I've caused awk to think I want the line above NODE_NAME too. Any ideas? Thanks in advance!