For the record, here's a
sed solution as well.
Code:
sed -n '/aaa/,$ {/aaa/,+3 d;p}' infile
It first matches from line aaa to the end, then from that selection it deletes the aaa line and the three following it, and prints the remainder.
Note that the
+" address option is, I believe, a
gnu sed extension, and other implementations likely don't have it.
And to round it off, here are two ways to do it in
ed too.
Code:
printf '%s\n' '1,/aaa/+3d' '%p' | ed -s infile
printf '%s\n' '/aaa/+4,$p' | ed -s infile
The first one deletes everything from line 1 to aaa+3, and prints what remains, while the second simply prints from line aaa+4 to the end (the rest of the lines are still in the buffer). As you can see,
ed has a more flexible address system than
sed.
If you replace the '
%p/p' commands with '
w', you can save the changes directly back to the input file. Or use '
w outfile' to write them to a new file. The first version is probably slightly better for this.
How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)