user630702
user630702

Reputation: 3147

Grep exact string not working when piped

If I cat text file containing mysqld_safe and mysqld then grep -w works as expected. But when piped with PS output it does not work.

ps -ef | grep  -w mysqld

Output's both lines.

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

I am expecting only mysqld. I'm aware of exclude option grep -v mysqld_safe.

Version - grep (GNU grep) 2.5.1

Upvotes: 2

Views: 339

Answers (1)

Sundeep
Sundeep

Reputation: 23667

If you have pgrep, using pgrep -x mysqld would be better choice than ps + grep

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

Upvotes: 1

Related Questions