如何在 Linux 中 grep 没有文件名的字符串?
我们知道我们可以使用grep命令在一个文件或多个文件的所有行中搜索特定的字符模式。grep命令对文件中的单词执行不区分大小写的搜索。
让我们看一个简单的例子,我们将在一个目录中存在的所有文件中grep一个模式。
命令
grep -i ‘lp’ Sample*输出结果
Sample: The sample was printed via the internet. Sample: I believe lp cares about what the device is. Sample1: This was printed via the internet. Sample1: I believe lp cares about what the device is. Sample2: This was printed via the internet. Sample3: I believe lp cares about what the device is.
这个特定场景的唯一问题是我们不希望文件名与输出一起打印。我们只需要找到特定模式的字符串,我们只需要打印该字符串。
在这种情况下,我们可以使用-h参数来隐藏文件名。
命令
grep -i -hn ‘lp’ Sample*
根据LinuxDocs,-h标志执行以下操作,
“-h,--no-filename
禁止输出文件名的前缀。当只有一个文件(或只有标准输入)要搜索时,这是默认设置。”
输出结果
The sample was printed via the internet. I Believe lp cares about what the device is. This was printed via the internet. I believe lp cares about what the device is. This was printed via the internet. I believe lp doesn't care what the device is
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短