我目前在一个目录中有 n 个数据文件,其中每个文件最多有 1 行很长的数据。我的目录结构是
director/
data1.json
data2.json
data3.json
Run Code Online (Sandbox Code Playgroud)
我知道这些文件中至少有一个包含我正在寻找的关键字,但是由于一行数据太长,它覆盖了我的整个终端。如何仅在执行关键字 grep 后获取文件名?我使用的 grep 命令是:
grep keyword *.json
Run Code Online (Sandbox Code Playgroud)
the*_*ive 33
-l 参数应该做你想做的。
-l, --files-with-matches
Suppress normal output; instead print the name of each input
file from which output would normally have been printed. The
scanning will stop on the first match. (-l is specified by
POSIX.)
Run Code Online (Sandbox Code Playgroud)
小智 9
您可以将 xargs 与 --null 与 grep 或 --print0 与 ack 结合使用。我发现ack速度更快。
grep -lr --null searchterm * | xargs -I {} -0 echo {}
ack -lr --print0 searchterm * | xargs -I {} -0 echo {}
归档时间: |
|
查看次数: |
11993 次 |
最近记录: |