linuxcommand-linetext-editored

How do I use relative line numbers with patterns in ed


In attempting to use ed to delete lines around a certain pattern I've been driving my self nut ts.

What I'd like to do is match a pattern, and then delete lines around it.

I've tried several variations

ed test.txt <<<< $'/pattern/-1,+1d\nwq'
ed test.txt <<<< $'(/pattern/-1,+1)d\nwq'
ed test.txt <<<< $'/pattern/-,+1d\nwq'
ed test.txt <<<< $'(/pattern/-,+1)d\nwq'
ed test.txt <<<< $'/pattern/-,+d\nwq'
ed test.txt <<<< $'(/pattern/-,+)d\nwq'

None of which worked. How is it done?


Solution

  • I figured it out after much trial and error, though I can't seem to find any documentation that would have told me this. It appears that each line reference must be made to a separate pattern match reference, and so the trick is to give the pattern twice.

     ed test.txt <<<< $'/pattern/-,/pattern/+d\nwq'