管道{gre}精确字符串无法正常工作

时间:2017-08-14 08:57:03

标签: grep

如果我cat包含mysqld_safemysqld的文本文件,则grep -w按预期工作。但是当使用PS输出管道时它不起作用。

ps -ef | grep  -w mysqld

输出两行。

/usr/bin/mysqld_safe
/usr/libexec/mysqld

我只期待mysqld。我知道排除选项grep -v mysqld_safe

版本 - grep (GNU grep) 2.5.1

1 个答案:

答案 0 :(得分:1)

如果你有pgrep,使用pgrep -x mysqld会比ps + grep更好的选择

来自man pgrep

   pgrep, pkill - look up or signal processes based on name and other attributes

   -x, --exact
          Only match processes whose names (or command line if -f is specified) exactly match the pattern.

   -l, --list-name
          List the process name as well as the process ID.  (pgrep only.)

   -c, --count
          Suppress  normal  output;  instead print a count of matching processes.  When count does not match any‐
          thing, e.g. returns zero, the command will return non-zero value.

   -n, --newest
          Select only the newest (most recently started) of the matching processes.