26

I have the following command : sed -i -e '/match1/,+2d' filex, which deletes 2 lines after finding the match "match1" in the file "file x". I want to add several matches to it, like match1, match 2 ....

So it will delete 2 lines after finding any of the matches, how can I achieve this ?

sehe
  • 374,641
  • 47
  • 450
  • 633
wael
  • 514
  • 2
  • 8
  • 17

3 Answers3

37

Two ways, depending upon the sed version and platform:

sed -e '/match1/,+2d' -e '/match2/,+2d' < oldfile > newfile

or

sed -e '/match1\|match2/,+2d' < oldfile > newfile
ziu
  • 2,634
  • 2
  • 24
  • 39
13

Not a sed user but it seems to me you could use :

sed -i -e '/(match1|match2)/,+2d' filex

Otherwise you could, if that's possible for you to do, do :

sed -i -e '/match1/,+2d' filex && sed -i -e '/match2/,+2d' filex

EDIT: Looks like I had the right idea but ziu got it.

shodanex
  • 14,975
  • 11
  • 57
  • 91
Max
  • 331
  • 1
  • 9
7

If I understand you correctly, you want

sed -e '/match1/,+2d' input.txt

For example, create input with seq 10 | sed '3i match1' > input.txt:

1
2
match1
3
4
5
6
7
8
9
10

The output of sed -e '/match1/,+2d' input.txt would be:

1
2
5
6
7
8
9
10
sehe
  • 374,641
  • 47
  • 450
  • 633