打印最后匹配的行? [英] Print last matching line?

查看:103
本文介绍了打印最后匹配的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用awk,我要打印文件的最后匹配行. 我只想要匹配的行本身,而不想要任何范围的行. 我可以使用这样的命令

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 this

awk '/foo/' bar.txt

但是,这将打印所有匹配的行,而不仅仅是最后一行.

However this will print all matching lines, not just the last one.

推荐答案

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

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

这篇关于打印最后匹配的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆