限制grep命令只打印一次文件名
我对在特定文件类型中查找字符串感兴趣。 下面的命令符合我的目的限制grep命令只打印一次文件名,grep,Grep,我对在特定文件类型中查找字符串感兴趣。 下面的命令符合我的目的 find /any/path -type f -name "*.log" | xargs grep -B2 -A2 'SUMMARY' {} \; 它给出以下输出: -- /path/to/file.log-line1 /path/to/file.log-line2 /path/to/file.log:text SUMMARY text /path/to/file.log-line1 /path/to/file.log-li
find /any/path -type f -name "*.log" | xargs grep -B2 -A2 'SUMMARY' {} \;
它给出以下输出:
--
/path/to/file.log-line1
/path/to/file.log-line2
/path/to/file.log:text SUMMARY text
/path/to/file.log-line1
/path/to/file.log-line2
--
我希望文件名不要在每行前面加上前缀。是否可能有如下输出
--
/path/to/file.log
line1
line2
text SUMMARY text
line1
line2
--
如果在linux和bash下运行此脚本,可以使用如下bash脚本:
#!/bin/bash
for fn in `grep --include \*.txt -lr 'SUMMARY' /any/path`; do
echo $fn
grep -A2 -B2 'SUMMARY' $fn
done
这将以递归方式从目录“/any/path”开始查找包含单词“SUMMARY”的所有文件。然后按名称打印所有匹配的文件,并使用第二行grep
打印匹配的部分