我正在经历一些事情,发现了我无法理解的事情,
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
一起使用时,还会输出用于过滤 ps -ef
输出的 grep
>。
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
关于UNIX grep 命令(grep -v grep),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29454101/