1

OK I found this question:

How do I delete a matching line, the line above and the one below it, using sed?

and just spent the last hour trying to write something that will match a string and delete the line containing the string and the line beneath it (or a variant - delete 2 lines beneath it).

I feel I'm now typing random strings. Please somebody help me.

Community
  • 1
  • 1

2 Answers2

4

If I've understood that correctly, to delete match line and one line after

/matchstr/{N;d;}

Match line and two lines after

/matchstr/{N;N;d;}
  • N brings in the next line
  • d - deletes the resulting single line
martin clayton
  • 76,436
  • 32
  • 213
  • 198
0

you can use awk. eg search for the word "two" and skip 2 lines after it

$ cat file
one
two
three
four
five
six
seven
eight
$ awk -vnum=2 '/two/{for(i=0;i<=num;i++)getline}1' file
one
five
six
seven
eight
ghostdog74
  • 327,991
  • 56
  • 259
  • 343