Linux 如何显示带有完整路径或文件名的 grep 结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13875380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show grep result with complete path or file name
提问by ?zzesh
I need to know the complete file path when I grep
.
我需要知道完整的文件路径grep
。
I use commands like
我使用类似的命令
cat *.log | grep somethingtosearch
Now what I need to show the result with complete file path from where the matched result were taken out.
现在我需要用完整的文件路径显示结果,从中取出匹配的结果。
Help anyone?
帮助任何人?
采纳答案by Kaadzia
Assuming you have two log-files in:
假设您有两个日志文件:
- C:/temp/my.log
- C:/temp/alsoMy.log
- C:/temp/my.log
- C:/temp/alsoMy.log
cd to C: and use:
cd 到 C: 并使用:
grep -r somethingtosearch temp/*.log
It will give you a list like:
它会给你一个列表,如:
temp/my.log:somethingtosearch
temp/alsoMy.log:somethingtosearch1
temp/alsoMy.log:somethingtosearch2
回答by dogbane
Use:
用:
grep somethingtosearch *.log
and the filenames will be printed out along with the matches.
并且文件名将与匹配一起打印出来。
回答by ?zzesh
For me
为了我
grep -b "searchsomething" *.log
worked as I wanted
如我所愿
回答by saeedgnu
If you want to see the full paths, I would recommend to cd
to the top directory (of your drive if using windows)
如果你想查看完整路径,我会建议到cd
顶级目录(如果使用 Windows,你的驱动器)
cd C:\
grep -r somethingtosearch C:\Users\Ozzesh\temp
Or on Linux:
或者在 Linux 上:
cd /
grep -r somethingtosearch ~/temp
if you really resist on your file name filtering (*.log) AND you want recursive
(files are not all in the same directory), combining find
and grep
is the most flexible way:
如果你真的在你的文件名过滤抵抗(* .LOG),并且希望recursive
(文件在同一目录不是全部),结合find
和grep
最灵活的方式:
cd /
find ~/temp -iname '*.log' -type f -exec grep somethingtosearch '{}' \;
回答by Andrii Abramov
The easiest way to print full paths is replace relative start path with absolute path:
打印完整路径的最简单方法是用绝对路径替换相对起始路径:
grep -r --include="*.sh" "pattern" ${PWD}
回答by BVB Media
Command:
命令:
grep -rl --include="*.js" "searchString" ${PWD}
Returned output:
返回的输出:
/root/test/bas.js
回答by Sergio
I fall here when I was looking exactly for the same problem and maybe it can help other.
当我正在寻找完全相同的问题时,我落到了这里,也许它可以帮助其他人。
I think the real solution is:
我认为真正的解决方案是:
cat *.log | grep -H somethingtosearch