sed or awk: delete n lines following a pattern

Martin DeMello picture Martin DeMello · Dec 9, 2010 · Viewed 93.6k times · Source

How would I mix patterns and numeric ranges in sed (or any similar tool - awk for example)? What I want to do is match certain lines in a file, and delete the next n lines before proceeding, and I want to do that as part of a pipeline.

Answer

dogbane picture dogbane · Dec 9, 2010

I'll have a go at this.

To delete 5 lines after a pattern (including the line with the pattern):

sed -e '/pattern/,+5d' file.txt

To delete 5 lines after a pattern (excluding the line with the pattern):

sed -e '/pattern/{n;N;N;N;N;d}' file.txt