Sed. Delete line before and after pattern.

Hi.
In need to delete line before and after pattern in file. I came to following commands.
Delete line before

sed -ni '/pattern/{x;d;};1h;1!{x;p;};${x;p;}' /etc/testfile

Delete line after

sed -i '/pattern/{N;s/\n.*//;}' /etc/testfile

Is it possible to merge it in single command?

Show the input you have and the output you want.

Try (not thoroughly tested):

sed -n '/pattern/{x;n;n};1h;1!{x;p;};${x;p;}' /etc/testfile
1 Like

Modifying your solution, try:

sed -n '/pattern/{x;n;d;};1h;1!{x;p;};${x;p;}' file