ps的grepping输出,排除单词grep [英] grepping output of ps, exclude the word grep
问题描述
我正在使用ps查找为执行命令"sleeping 1234&"而创建的进程的pid. 我grep结果只匹配"sleeping 1234".
I'm using ps to find the pid of a process created to execute the command "sleep 1234 &" I grep the result to match only "sleep 1234".
ps -A -f | grep "sleep 1234"
但是,这也与命令"grep sleep 1234"本身匹配,返回两行而不是一行.如何为grep编写模式以排除单词"grep"本身?
however, this matches also the command "grep sleep 1234" itself, returning two lines instead of one. How do I write a pattern for grep to exclude the word 'grep' itself?
谢谢
推荐答案
这是一个很常见的问题,最简单的解决方案是在grep'ed模式中用方括号括住一个字符:
This is a pretty common problem and the easiest solution is to just surround a character in the grep'ed pattern with square brackets:
ps -A -f | grep "[s]leep 1234"
现在它将匹配sleep 1234
,但不匹配[s]leep 1234
(由于s
和l
之间的文字]
),并且grep行不再匹配.
This will now match sleep 1234
, but not [s]leep 1234
(because of the literal ]
between s
and l
), and the grep line no longer matches.
grep
出现在进程列表中的原因是流水线是从右到左执行的,因此grep
实际上是在ps
之前执行的.
The reason that the grep
is in the process list is that pipelines are executed from right to left, so the grep
is actually executed prior to the ps
.
这篇关于ps的grepping输出,排除单词grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!