0

When performing a grep filtering across pipes, as in the following example,

$ ps -ef | grep 24604 
username   18660 24604  0 16:38 pts/3    00:00:00 ps -ef
username   18661 24604  0 16:38 pts/3    00:00:00 grep --color=auto 24604
username   24604 23548  0 Aug13 pts/3    00:00:00 /bin/bash

the grep command itself is part of the results, and usually - I filter it out with another grep -v, viz.:

$ ps -ef | grep 24604 | grep -v grep
username   20375 24604  0 16:40 pts/3    00:00:00 ps -ef
username   24604 23548  0 Aug13 pts/3    00:00:00 /bin/bash

Is there a grep argument which will filter out the grep string?
(Or any other trick to avoid the grep -v?)

boardrider
  • 949
  • 2
  • 18
  • 29

2 Answers2

3

This is typically handled by putting brackets around the first character:

$ ps auxw | grep [c]ron
root      5710  0.0  0.0  10640  1800 ?        Ss   14:41   0:00 /usr/sbin/cron
$

This works because cron no longer appears in greps command line ([c]ron does). A more modern approach is to use the pgrep command if available.

Mark Wagner
  • 18,019
  • 2
  • 32
  • 47
2

How about:

pgrep -l 24604

It really depends on the usage, but pgrep is usually more suited for automation which is probably what you're trying to do.

ETL
  • 6,513
  • 1
  • 28
  • 48