UNIX grep命令(grep -v grep) [英] UNIX grep command (grep -v grep)
问题描述
grep -v grep
这是什么意思?我知道 -v
开关将选择所有不匹配的行。但为什么第二个 grep
?
这是完整的命令:
ps -ef | grep rsync -avz \
| grep oradata $ {DAY} _ [0-1] [0-9] \
| grep -v grep \
| awk'{print $ 2}'| wc -l
grep
与 ps -ef
一起使用时,还会输出用于过滤<$的输出的 grep
c $ c> ps -ef 。
grep -v grep
表示不包含 grep
用于在命令输出中进行过滤。
您也可以通过使用 grep
code> regex 模式。
例如,在下面的例子中,您将不需要 grep -v grep
来避免 grep
在输出中:
ps -ef | grep [r] sync
下面是另一个示例,显示不同的命令及其输出,注意第一个 grep
也在输出中,而最后两个 grep
不会打印在输出中:
$ ps -ef | grep ipython
501 18055 18031 0 12:44 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:44 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:44 ttys000 0:00.00 grep ipython
$ ps -ef | grep ipython | grep -v grep
501 18055 18031 0 12:44 ttys000 0:00.00 / bin / bash /Users/amit/anaconda/bin/python.app / Users / amit / anaconda / bin / ipython notebook --profile =海洋
501 18056 18055 0 12:44 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:44 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:44 ttys000 0:00.85 /Users/amit/anaconda/python.app/Contents/MacOS/python/Users/amit/anaconda/bin/ipython notebook --profile = ocean
I was going through something and found this which I could not understand,
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
?
This is the full command:
ps -ef | grep rsync -avz \
| grep oradata${DAY}_[0-1][0-9] \
| grep -v grep \
| awk '{print $2}' | wc -l
grep
when used with ps -ef
also outputs the grep
used for filtering the output of ps -ef
.
grep -v grep
means that do not include the grep
used for filtering in the command output.
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
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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!