bash - grep 文件列表中的最后一个匹配项
问题描述
我在同一个文件夹下有很多文件:
uut01_010203030.txt
uut02_020038300.txt
uut03_202002003.txt
我可以单独 grep 最后一次出现:
tac uut01_*.txt | grep -m 1 'some_pattern'
我可以将文件提供给 grep 文件中第一次出现的模式:
ls uut*.txt | xargs grep -m 1 'some_pattern'
但是,我不能真正将 tac 和 grep 结合在一起,以下命令给了我错误:
ls uut*.txt | xargs tac | grep -m 1 'some_pattern'
xargs: tac: terminated by signal 13
我能做些什么来解决这个问题?
解决方案
$ tail -n +1 file*
==> file1 <==
1
2
3
==> file2 <==
1
2
3
4
5
==> file3 <==
1
2
3
4
$ grep -H '.' file* | tac | awk -F':' '!seen[$1]++'
file3:4
file2:5
file1:3
$ awk '/./{hits[FILENAME]=$0} END{for (f in hits) print f, hits[f]}' file*
file1 3
file2 5
file3 4
推荐阅读
- javascript - 使用服务进行http响应错误时如何访问“this”
- java - 如何下载视频使其对应用程序私有(Android)
- html - 在新部分中创建的 CSS p 元素从具有自己的类名的不同部分继承属性
- sql - 如何从 BigQuery 表中获取与另一个表列相匹配的值的相应列加入?
- asp.net - 为什么生成的查询使用 [Project1]?
- python - 如何调整列表以使某些元素位于同一索引上?
- python - 使用学生姓名获取学生成绩
- java - 在 Spring 中安排批量邮件作业
- rust - 无法运行 bellperson crate -“无法为 `fff v0.2.2` 运行自定义构建命令”
- php - 在 apache ldap.conf 中设置连接属性后,如何在 PHP 中实现使用 LDAP 池连接?