如何使用 grep 进行匹配但不打印匹配项? [英] How can I use grep to match but without printing the matches?
问题描述
我是 linux shell 的新手,我正在尝试这样做,最好在一行中,条件如下:它无法向终端输出任何内容.
I'm new to linux shell and am trying to do this, preferably in one line, with the following condition: It can't output anything to the terminal.
/var/folder/program.exe -L parameters | grep text_to_filter && echo SomeText >'/tmp/Log.txt'
问题是 .exe 将 XML 数据输出到终端.我不知道如何 grep,使用退出状态,但不会让屏幕被每个匹配的输出弄得乱七八糟.如果我使用 /dev/null 2>&1
,它会很好地通过管道传输数据,但是我无法 grep 数据.有什么想法吗?
The problem is the .exe spits out XML data to terminal. I can't figure out how to grep, use the exit status, but not have the screen cluttered with the output of each match. If I use /dev/null 2>&1
, it pipes it quite but then I can't grep the data. Any idea's?
推荐答案
使用 grep -q
(quiet)
Use grep -q
(quiet)
/var/folder/program.exe -L parameters |
grep -q "text_to_filter" && echo 'SomeText' > '/tmp/Log.txt'
根据man grep
:
-q、--quiet、--silent
安静的;不要向标准输出写入任何内容.如果找到任何匹配项,则立即以零状态退出,即使出现错误被检测到.另见-s 或 --no-messages 选项.
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option.
这篇关于如何使用 grep 进行匹配但不打印匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!