Linux 查找返回字符串行号的命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7600313/
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
Find Command that returns line number of the string
提问by nikhil
I have a bunch of files organized into directories..All these are text files (c/c++). I am trying to understand this code and i need to look at the declarations of many variables..How can i use find command to get the exact location( File Name with line number(s) ) using Find command in ubuntu linux?? Or is there any graphical tool for doing the same?
我有一堆文件组织成目录..所有这些都是文本文件(c/c++)。我试图理解这段代码,我需要查看许多变量的声明..如何使用 find 命令在 ubuntu linux 中使用 Find 命令获取确切位置(带有行号的文件名)?或者是否有任何图形工具可以做同样的事情?
回答by arunkumar
You can do this with grep. grep -n 'search-term' *.c
will give you the filename and line number where the term appears.
你可以用 grep 做到这一点。grep -n 'search-term' *.c
将为您提供该术语出现的文件名和行号。
回答by ring bearer
find . -name *.c -exec grep -Hn "your search term here" {} \;
If you really want to use find
.
如果你真的想使用find
.
EDIT
编辑
explanation
解释
find . -name *.c
- find files in current dir and below where name is like *.c
find . -name *.c
- 在当前目录和下面查找名称类似于 *.c 的文件
-exec
- execute command that follows
-exec
- 执行下面的命令
grep -Hn
- grep and print results with file name and line number of match
grep -Hn
- grep 并打印带有文件名和匹配行号的结果
{} \;
- {} marks where the name of each file found will be substituted and the backslash-
semicolon marks the end of the command to execute.
{} \;
- {} 标记找到的每个文件的名称将被替换的位置,反斜杠分号标记要执行的命令的结束。