How can I delete a line containing a matching pattern and the following n lines using a tool supporting regular expressions?
Said differently, how can I write a regular expression matching a line containing a matching pattern and the following n lines, so that I can replace them with nothing?
For example, if I have a matching pattern bbbb
and I want to delete also the 5 lines that follows it, for the input file:
aldjflajdkl
aaaabbbbaaaa
1l;adfjl
2aldfjl
3adlflkdas
4aldfjd
5aldfkld
6dlafjlkdas
The output would be:
aldjflajdkl
6dlafjlkdas
It probably simplify things that in my specific case, it cannot be that the matching pattern (bbbb
) is contained in the following 5 lines.
A solution already exists for sed, but it relies only partially on regular expressions, and uses custom replacement commands which are not portable.
sed "s/a/z/g" file.ext
replaces 'a' with 'z', and does it with all occurrences of 'a'. The regex is in the find portion of that, that's where the 'a' is. Though sed can't see new lines in the find portion and some more advanced regex features are missing from it, so there are better tools than sed for regex support.