使用grep时获取行号 [英] Get line number while using grep
问题描述
我正在使用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:目前我得到的是/var/www/file.php:$ options ="this.target"
,但是我要获取的是/var/www/file.php:1142 $ options ="this.target";
,其中 1142
是包含该字符串的行号.
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.
我用来递归grep的语法是 sudo grep -r'pattern''/var/www/file.php'
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 -n SEARCHTERM file1 file2 ...
这篇关于使用grep时获取行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!