如何使用grep进行匹配但不打印匹配项? [英] How can I use grep to match but without printing the matches?

查看:763
本文介绍了如何使用grep进行匹配但不打印匹配项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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(安静)

/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆