SED: addressing two lines before match

后端 未结 6 1714
梦如初夏
梦如初夏 2021-01-20 05:20

Print line, which is situated 2 lines before the match(pattern).

I tried next:

sed -n \': loop
/.*/h
:x
{n;n;/cen/p;}
s/./c/p
t x
s/n/c/p
t loop
{g;p         


        
6条回答
  •  醉酒成梦
    2021-01-20 06:08

    This one with grep, a bit simpler solution and easy to read [However need to use one pipe]: grep -B2 'pattern' file_name | sed -n '1,2p'

提交回复
热议问题