本文介绍了UNIX grep 命令 (grep -v grep)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was going through something and found this which I could not understand,

grep -v grep

这意味着什么?我知道 -v 开关将选择所有不匹配的行.但是为什么是第二个 grep?

What does this signify? I know that -v switch will select all the lines that do not match. But why the second grep?

这是完整的命令:

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

推荐答案

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

grep when used with ps -ef also outputs the grep used for filtering the output of ps -ef.

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

grep -v grep means that do not include the grep used for filtering in the command output.

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

You can also avoid grep in the results by using a regex pattern.For example, in the following example you won't need a grep -v grep to avoid grep in the output:

ps -ef | grep [r]sync

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

Here's another example, showing different commands and their output, notice the first one where grep is also in the output whereas in the last two grep is not printed in the output:

$ 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

这篇关于UNIX grep 命令 (grep -v grep)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:20
查看更多