grep last match and it's following lines

后端 未结 2 1817
刺人心
刺人心 2021-02-09 09:58

I\'ve learnt how to grep lines before and after the match and to grep the last match but I haven\'t discovered how to grep the last match

2条回答
  •  天命终不由人
    2021-02-09 10:08

    using awk instead:

    awk '/pattern/{m=$0;l=NR}l+1==NR{n=$0}END{print m;print n}' foo.log
    

    small test, find the last line matching /8/ and the next line of it:

    kent$  seq 20|awk '/8/{m=$0;l=NR}l+1==NR{n=$0}END{print m;print n}'
    18
    19
    

提交回复
热议问题