我使用grep递归来搜索文件中的字符串,所有匹配的文件和包含该字符串的行都在终端上打印.但是有可能获得这些线的行号吗?
例如:目前我得到的是/var/www/file.php: $options = "this.target",但我想要得到的是/var/www/file.php: 1142 $options = "this.target";,1142包含该字符串的行号在哪里.
我用来递归grep的语法是 sudo grep -r 'pattern' '/var/www/file.php'
还有一个问题是,我们如何得到不等于模式的结果.像所有文件一样,但不是那些有特定字符串的文件?
Mir*_* A. 516
grep -nr SEARCHTERM file1 file2 ...
Run Code Online (Sandbox Code Playgroud)
car*_*ito 143
行号打印有grep -n:
grep -n pattern file.txt
Run Code Online (Sandbox Code Playgroud)
要仅获取行号(没有匹配的行),可以使用cut:
grep -n pattern file.txt | cut -d : -f 1
Run Code Online (Sandbox Code Playgroud)
不包含图案的线条打印有grep -v:
grep -v pattern file.txt
Run Code Online (Sandbox Code Playgroud)
小智 23
如果您只想要行号,请执行以下操作:
grep -nr Pattern file.ext | gawk '{print $1}' FS=":"
Run Code Online (Sandbox Code Playgroud)
例:
$ grep -nr 9780545460262 EXT20130410.txt | gawk '{print $1}' FS=":"
48793
52285
54023
Run Code Online (Sandbox Code Playgroud)
小智 6
grep -A20 -B20 pattern file.txt
Run Code Online (Sandbox Code Playgroud)
搜索模式并在模式之前和之前显示20行