Remove line containing certain string and the following line
I use this
cat foo.txt | sed '/bar/d'
to remove lines containing the string
barin the file.
I would like however to remove those lines and the line directly after it. Preferably in
awkor other tool that's available in MinGW32.
It's a kind of reverse of what I can get in
-Bto print matching lines as well as lines before/after the matched line.
Is there any easy way to achieve it?
If you have GNU sed (so non-embedded Linux or Cygwin):
sed '/bar/,+1 d'
If you have
baron two consecutive lines, this will delete the second line without analyzing it. For example, if you have a 3-line file
fooline will stay.
+1 for the length :) In my particular example I don't have consecutive `bar`s so this one is super easy to remember.
`sed '/bar/d'` if you just want to "Remove line containing certain string" and **not** the next.
@Gilles Thanks that worked, but I might have done some mistake and asked the question.
To break down the command: the first part `/bar/,+1/` is a sed address (https://www.gnu.org/software/sed/manual/sed.html#sed-addresses) that says start at the regexp `/bar/`, then a `,` as a separator between start/end addresses, and end `+1` lines (from the start). Then the `d` is the command (delete)
I actually wanted to delete the matching line so I just replaced +1 with +0 and it worked! like: sed '/bar/,+0 d'