Grep精确字符串在管道中不工作

Grep精确字符串在管道中不工作,grep,Grep,如果我cat包含mysqld\u safe和mysqld的文本文件,那么grep-w将按预期工作。但是,当使用PS输出进行管道传输时,它不起作用 ps -ef | grep -w mysqld 输出是两行的 /usr/bin/mysqld_safe /usr/libexec/mysqld 我只需要mysqld。我知道排除选项grep-v mysqld\u safe Version-grep(GNU-grep)2.5.1如果您有pgrep,使用pgrep-x mysqld将是比ps+grep

如果我
cat
包含
mysqld\u safe
mysqld
的文本文件,那么
grep-w
将按预期工作。但是,当使用
PS
输出进行管道传输时,它不起作用

ps -ef | grep  -w mysqld
输出是两行的

/usr/bin/mysqld_safe
/usr/libexec/mysqld
我只需要
mysqld
。我知道排除选项
grep-v mysqld\u safe


Version-
grep(GNU-grep)2.5.1

如果您有
pgrep
,使用
pgrep-x mysqld
将是比
ps+grep
更好的选择

   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.
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.

嗯,
grep'\bmysqld\b'
mysqld\u-safe
中找不到
mysqld
。因为
是一个单词char,并且
d
之间没有单词边界。假设bash,您必须单引号引用正则表达式。目前,grep看到的regex2是“bmysqlb”,不应该与这些行中的任何一行匹配。这不是家庭作业,我正在努力解决监控问题。我想先过滤这个,然后过滤软件的json输出。另外,这是我在这里发布问题前几分钟学到的东西。有很多类似的问题,我已经讨论了其中的大部分,这就是我如何跨越grep边界的。如果你有pgrep,我认为使用
pgrep-x mysqld
ps+grep
pgrep更好。谢谢你的建议。