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

被刻印的时光 ゝ 提交于 2020-06-13 15:37:13

问题


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'

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?


回答1:


Use grep -q (quiet)

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

As per man grep:

-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.




回答2:


Try using |& rather than just |. (needs bash 4)



来源:https://stackoverflow.com/questions/19841812/how-can-i-use-grep-to-match-but-without-printing-the-matches

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!