简体   繁体   English

如何使用 grep 进行匹配但不打印匹配项?

[英]How can I use grep to match but without printing the matches?

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.我是 linux shell 的新手,我正在尝试这样做,最好是在一行中,但条件如下:它不能 output 对终端有任何影响。

/var/folder/program.exe -L parameters | grep text_to_filter && echo SomeText >'/tmp/Log.txt'

The problem is the.exe spits out XML data to terminal.问题是 .exe 向终端吐出 XML 数据。 I can't figure out how to grep, use the exit status, but not have the screen cluttered with the output of each match.我不知道如何使用 grep,使用退出状态,但屏幕上不会出现每场比赛的 output 混乱。 If I use /dev/null 2>&1 , it pipes it quite but then I can't grep the data.如果我使用/dev/null 2>&1 ,它会很好地传输它,但是我不能 grep 数据。 Any idea's?有任何想法吗?

Use grep -q (quiet)使用grep -q (静音)

/var/folder/program.exe -L parameters |
grep -q "text_to_filter" && echo 'SomeText' > '/tmp/Log.txt'

As per man grep :根据man grep

-q, --quiet, --silent Quiet; -q, --quiet, --silent安静; do not write anything to standard output.不要向标准 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.另请参阅 -s 或 --no-messages 选项。

Try using |& rather than just |.尝试使用 |& 而不仅仅是 |。 (needs bash 4) (需要bash 4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM