grepping output of ps, exclude the word grep [duplicate]

走远了吗. 提交于 2019-12-03 18:18:58

问题


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"

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?

Thanks


回答1:


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"

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.

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.



来源:https://stackoverflow.com/questions/51178976/grepping-output-of-ps-exclude-the-word-grep

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!