Print last matching line?

后端 未结 5 770
無奈伤痛
無奈伤痛 2021-01-14 08:20

Using awk, I would like to print the last matching line of a file. I would like only the matching line itself, not any range of lines. I can use a command like

5条回答
  •  被撕碎了的回忆
    2021-01-14 08:53

    sed solution:

    sed -n '/foo/h;$!b;g;p' bar.txt
    

    Place the matching regex in hold buffer and branch out. Keep doing this until end of file. When the end of file is reached, grab the line from hold space and place it on pattern space. Print pattern space.

提交回复
热议问题