使用grep时获取行号
我使用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' 
还有一个问题是,我们如何得到不等于模式的结果。 像所有的文件,但不是有一定的字符串?
grep -nr SEARCHTERM file1 file2 ...
  行号用grep -n打印: 
grep -n pattern file.txt
  为了只获得行号(没有匹配的行),可以使用cut : 
grep -n pattern file.txt | cut -d : -f 1
  不包含模式的行用grep -v打印: 
grep -v pattern file.txt
如果您只想要行号,请执行以下操作:
grep -nr Pattern file.ext | gawk '{print $1}' FS=":"
例:
$ grep -nr 9780545460262 EXT20130410.txt | gawk '{print $1}' FS=":" 
48793
52285
54023
上一篇: Get line number while using grep
下一篇: How to grep (search) committed code in the git history?
