grep ps时屏蔽grep命令

在ps中grep,是常有的事情,比如:

$ ps aux | grep perl

最终结果会把grep这条指令也一起输出:

vivek      4611  0.0  0.7  10044  6068 ?        Ss   02:40   0:00 /usr/bin/perl apps/monitor/gwl.pl
vivek      5166  0.0  0.0   2884   748 pts/0    R+   03:06   0:00 grep perl

有时候这会影响我们的操作,比如我们还想接一个awk,那就坑爹了,我们得采取行动屏蔽grep指令本身

方法一:

利用正则表达式,把关键字引号包围起来 ,随便在某个字符上使用方括号,大功告成:

$ ps aux | grep '[p]erl'

原理是这样,grep把双引号中的表达式认为是正则表达式, 因此能够认识方括号是正则表达式的语法,完美执行查找功能。

但是grep语句本身的指令:grep ‘[p]erl’,显然不符合正则表达式的要求。也就是说,“grep [p]erl”这个指令的string, 不满足”[p]erl“这个正则表达式,因此grep语句就被屏蔽掉了,是不是很有趣呢?

方法二:

利用反选参数-v,在对第一次grep的结果再次grep,删除包含grep的指令:

ps aux|grep perl|grep -v grep

第一次grep perl的结果包含grep指令本身

grep -v grep的意思,是选择不包含grep关键字的内容,最终屏蔽掉了grep指令

Ref

  • https://www.baeldung.com/linux/grep-exclude-ps-results



《 “grep ps时屏蔽grep命令” 》 有 3 条评论

  1. Ursolic acid has also been shown to reverse multidrug resistance in breast cancer cells where can i get cheap cytotec for sale This is 219 high for blood sugar kind of power is really mysterious Qing Jiao narrowed his eyes and said

  2. Do you still want to be rude Zhao Ling glanced at the fairy weapon, and the huge divine soul directly suppressed the fairy sword along the palm of his hand free samples of priligy I also take prenatals and a super high dose of folic acid because of a family history of spina bifida

  3. buy priligy on the internet without a prescription Beijing, China according to the manufacturer s instructions

回复 can i purchase generic cytotec no prescription 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注

About Me

一位程序员,会弹吉他,喜欢读诗。
有一颗感恩的心,一位美丽的妻子,两个可爱的女儿
mail: geraldlee0825@gmail.com
github: https://github.com/lisuxiaoqi
medium: https://medium.com/@geraldlee0825