(标准输入)用 grep -iHn "ERROR" 显示而不是文件名,第二个 grep -vH "filter" 显示(标准输入),不再是文件名

问题描述 投票:0回答:1

我尝试做的事情:

  • 查找当天的所有日志文件
  • 然后在文件中 grep "ERROR"
  • 然后 grep -v "Not this line contains the filter" 作为第一个 grep 命令结果中的过滤器
  • 第一个 grep 命令显示文件名/文件路径和预期的行
  • 第二个 grep -v 命令不再显示它显示的文件名 $>(标准输入):....

问题,删除该行中包含“34”的所有结果行

[tom@centos7-home-dell ~]$ find /home/tom/tmp/test_grep -name "*.log" -type f | xargs grep -i ERROR 
/home/tom/tmp/test_grep/file1.log:ERROR:12;34
/home/tom/tmp/test_grep/file1.log:ERROR:12
/home/tom/tmp/test_grep/file2.log:ERROR:67890
/home/tom/tmp/test_grep/file2.log:ERROR:12;34
/home/tom/tmp/test_grep/file2.log:ERROR:12
[tom@centos7-home-dell ~]$ find /home/tom/tmp/test_grep -name "*.log" -type f | xargs grep -i ERROR | grep -vH "34"
(Standardeingabe):/home/tom/tmp/test_grep/file1.log:ERROR:12
(Standardeingabe):/home/tom/tmp/test_grep/file2.log:ERROR:67890
(Standardeingabe):/home/tom/tmp/test_grep/file2.log:ERROR:12

我的解决方案显示了预期的结果,但我不确定它是否可以。因为我使用 xargs -0 命令。请看这里:

[thor@centos7-home-dell wildfly]$ find /home/thor/tmp/test_grep -name "*.log" -type f | xargs grep -i ERROR
/home/thor/tmp/test_grep/file1.log:ERROR:12;34
/home/thor/tmp/test_grep/file1.log:ERROR:12
/home/thor/tmp/test_grep/file2.log:ERROR:67890
/home/thor/tmp/test_grep/file2.log:ERROR:12;34
/home/thor/tmp/test_grep/file2.log:ERROR:12

[thor@centos7-home-dell wildfly]$ find /home/thor/tmp/test_grep -name "*.log" -type f | xargs grep -i ERROR | xargs -0 | grep -v 34
/home/thor/tmp/test_grep/file1.log:ERROR:12
/home/thor/tmp/test_grep/file2.log:ERROR:67890
/home/thor/tmp/test_grep/file2.log:ERROR:12

bash grep find
1个回答
1
投票

我想你想做的就是这个:

find /home/tom/tmp/test_grep -name '*.log' -type f -exec \
awk -v OFS=':' 'toupper($0) ~ /ERROR/ && !/34/ { print FILENAME, $0 }' {} +

您现有的代码以及上面复制的逻辑将排除其中包含包含

34
的数字的行,例如:

/home/thor/tmp/test_grep/file2.log:ERROR:67340

我怀疑这不是你真正想要发生的事情。鉴于此,您可能需要将

/34/
正则表达式更改为
/[:;]34(;|$)/
或类似的值以避免错误匹配,具体取决于您真正想要做什么。

© www.soinside.com 2019 - 2024. All rights reserved.