从多个文件的 grep 搜索中获取最后一行 [英] get last line from grep search on multiple files
本文介绍了从多个文件的 grep 搜索中获取最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前在使用 grep 命令时遇到了一些问题.
I'm curently having some problem with a grep command.
我找到了只显示 grep 搜索的最后一行的方法:
I've found the way to only show the last line of a grep search :
grep PATERN FILE_NAME | tail -1
我还找到了在多个选定文件中进行 grep 搜索的方法:
I also find the way to make a grep search in multiple selected files :
find . -name "FILE_NAME" | xargs -I name grep PATERN name
现在我只想获取每个文件的 grep 结果的最后一行.我试过这个:
Now I would like to only get the last line of the grep result for each single file. I tried this :
find . -name "FILE_NAME" | xargs -I name grep PATERN name | tail -1
这仅返回最后一个文件的最后一个值,我希望每个文件都有最后一个匹配模式.
This returns me only the last value of the last file where I would like to have the last matching patern for every file.
推荐答案
for f in $(find . -name "FILE_NAME"); do grep PATTERN $f | tail -1; done
这篇关于从多个文件的 grep 搜索中获取最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文