使用 grep 时获取行号 [英] Get line number while using grep

查看:21
本文介绍了使用 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??

例如:目前我得到的是 /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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆