如何在 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 红包祝福语简短10字
18 周六早晨祝福语简短