how to delete lines according to a pattern in awk
I need to delete(or skip) next three lines after a pattern is found in some line of the file.
Does anybpdy have an idea how to do that in awk? thanks |
Use the getline function three times
|
Hi.
If you don't mind using sed, Code:
$ echo -e '1\n2\n3\n4\n5\n6' | sed '/2/{n;N;N;d}' Quote:
In GNU sed the latter can be done also as follows Code:
$ echo -e '1\n2\n3\n4\n5\n6' | sed '/2/,+3 d' |
Record NR value plus 3 at point of finding what you want and only print more details when you get to corresponding NR value
|
Human languages are sometimes frustratingly vague. You thought you stated your desire accurately, but there are four solutions with different results that all do what you stated you want.
If you want to keep the line containing the matching pattern, but omit the three next lines, use Code:
awk ' { print } If you want to the three lines including the one containing the pattern, use Code:
awk '/pattern/ { getline ; getline ; getline ; next } If you want to check if the omitted lines trigger further omissions, use Grail's approach: Code:
awk '/pattern/ { nNR = NR + 3 } Code:
awk ' NR >= nNR { print } |
All times are GMT -5. The time now is 07:02 PM. |