How to delete a line matching a pattern and a line after it using sed?

I have a file that looks something like this:

good text good text FLAG bad text bad text good text good text good test bad Text FLAG bad text bad text good text 

I need to delete any line containing "FLAG", and I always need to delete one line immediately after the line "FLAG".

"FLAG" lines are found quite irregularly, and I can not rely on any strategy with line numbers.

Does anyone know how to do this with sed?

+6
source share
3 answers

Using the GNU extension version:

 sed -e '/FLAG/,+1 d' infile 

This gives:

 good text good text good text good text good test good text 
+8
source

This works and does not depend on any extensions:

 sed '/FLAG/{N d }' infile 

N reads the next line in the pattern space, then d deletes the pattern space.

+9
source

Here is one way: awk :

 awk '/FLAG/{f=1;next}f{f=0;next}1' file 

or

 awk '/FLAG/{getline;next}1' file 
+2
source

Source: https://habr.com/ru/post/950400/


All Articles