2

Good day,

I was wondering how to delete to consecutive lines when this pattern is found: /^pt [a-z]\npy [0-9][0-9][0-9][0-9]\n//

Expected Input

pt a
py 01234
pt b
cd abc
py 5678

Expected Output

pt b
cd abc
py 5678

Thank so much in advance for any clue.

2 Answers2

3

This should work:

sed '/^pt [a-z]/{N;/py [0-9][0-9][0-9][0-9]/d}' your_file

Expanation

  • If the current line matches /^pt [a-z]/ we execute what's between braces.
  • N appends the next line to the active buffer.
  • If the active buffer now matches /py [0-9][0-9][0-9][0-9]/, we delete (i.e. we don't print) the contents of the active buffer. This is accomplished by d.
Joseph R.
  • 39,549
0

Through Perl,

$ perl -00pe 's/^pt [a-z]\npy [0-9]{5}\n//' file
pt b
cd abc
py 5678
Avinash Raj
  • 3,703