Reputation: 171
I want to use awk to print lines that match a pattern only if the following line does not match the pattern. In this case, the pattern is that the line begins with O. This is what I tried:
awk '!/^O/ {print x}; /^O/ {x=$0}' myfile.txt
This is printing far too many lines though, including printing the lines I specifically do not want to print.
Upvotes: 0
Views: 868
Reputation:
Not tested. Should probz work
awk '/^O/{if(seen==0){seen=1};c=$0} !/^O/{if (seen==1) {print c; seen=0;}}' myfile.txt
Shortened version
awk '/^O/{x=$0} !/^O/{if(x!=0) {print x; x=0;}}' myfile.txt
More shortening
awk '/^O/{x=$0} !/^O/{if(x){print x;x=0;}}' myfile
Think this is shortest it can go
awk '/^O/{x=$0} !/^O/&&x{print x;x=0;}' myfile
Changed them all because it printed the wrong lines.
also made it shorter :)
awk 'a=/^O/{x=$0} !a&&x{print x;x=0;}' myfile
Upvotes: 1