且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

打印最后匹配的行?

更新时间:2022-03-19 23:26:02

您可以将值保存在变量中,然后在处理整个文件后将其打印出来:

You can save the value in a variable and then print it after processing the whole file:

awk '/foo/ {a=$0} END{print a}' file

测试

$ cat file
foo1
foo2
foo3
4
5
$ awk '/foo/ {a=$0} END{print a}' file
foo3