Get line number while using grep
I am using grep recursive to search files for a string, and all the matched files and the lines containing that string are print on the terminal. But is it possible to get the line numbers of those lines too??
ex: presently what I get is /var/www/file.php: $options = "this.target"
, but what I am trying to get is /var/www/file.php: 1142 $options = "this.target";
, well where 1142
would be the line number containing that string.
Syntax I am using to grep recursively is sudo grep -r 'pattern' '/var/www/file.php'
One more question is, how do we get results for not equal to a pattern. Like all the files but not the ones having a certain string?
grep -nr SEARCHTERM file1 file2 ...
Line numbers are printed with grep -n
:
grep -n pattern file.txt
To get only the line number (without the matching line), one may use cut
:
grep -n pattern file.txt | cut -d : -f 1
Lines not containing a pattern are printed with grep -v
:
grep -v pattern file.txt
If you want only the line number do this:
grep -nr Pattern file.ext | gawk '{print $1}' FS=":"
Example:
$ grep -nr 9780545460262 EXT20130410.txt | gawk '{print $1}' FS=":"
48793
52285
54023
链接地址: http://www.djcxy.com/p/3316.html
上一篇: grep,但只有某些文件扩展名
下一篇: 使用grep时获取行号