UNIX grep 命令(grep -v grep)

UNIX grep command (grep -v grep)

我正在经历一些事情,发现这个我无法理解,

grep -v grep

这是什么意思?我知道 -v 开关将 select 所有不匹配的行。但是为什么第二个grep

这是完整的命令:

ps -ef | grep rsync -avz \
| grep oradata${DAY}_[0-1][0-9] \
| grep -v grep \
| awk '{print }' | wc -l

grepps -ef 一起使用时也会输出 grep 用于过滤 ps -ef 的输出。

grep -v grep表示在命令输出中不包含用于过滤的grep

您还可以使用 regex 模式避免结果中出现 grep。 例如,在以下示例中,您不需要 grep -v grep 来避免输出中出现 grep

ps -ef | grep [r]sync

这是另一个示例,显示了不同的命令及其输出,请注意第一个 grep 也在输出中,而在最后两个 grep 未打印在输出中:

$ ps -ef | grep ipython
  501 18055 18031   0 12:44AM ttys000    0:00.00 /bin/bash /Users/amit/anaconda/bin/python.app /Users/amit/anaconda/bin/ipython notebook --profile=ocean
  501 18056 18055   0 12:44AM ttys000    0:00.85 /Users/amit/anaconda/python.app/Contents/MacOS/python /Users/amit/anaconda/bin/ipython notebook --profile=ocean
  501 18067 18031   0 12:44AM ttys000    0:00.00 grep ipython

$ ps -ef | grep ipython | grep -v grep
  501 18055 18031   0 12:44AM ttys000    0:00.00 /bin/bash /Users/amit/anaconda/bin/python.app /Users/amit/anaconda/bin/ipython notebook --profile=ocean
  501 18056 18055   0 12:44AM ttys000    0:00.85 /Users/amit/anaconda/python.app/Contents/MacOS/python /Users/amit/anaconda/bin/ipython notebook --profile=ocean

$ ps -ef | grep [i]python
  501 18055 18031   0 12:44AM ttys000    0:00.00 /bin/bash /Users/amit/anaconda/bin/python.app /Users/amit/anaconda/bin/ipython notebook --profile=ocean
  501 18056 18055   0 12:44AM ttys000    0:00.85 /Users/amit/anaconda/python.app/Contents/MacOS/python /Users/amit/anaconda/bin/ipython notebook --profile=ocean