Username/Email:  Password: 
TwitterFacebookFlickrRSS

Get the grep Out!

While using grep to search for a running process from a ps ax output, it also includes the grep command, but in some cases (for example in a script) this is undesirable. To have a 'clean' output you can use brackets in this way for the grep command:

$ ps ax | grep s[s]h
4920 ?    Ss 0:00   /usr/sbin/sshd
$ 

This Tech Tip was brought to us by Alessandro in Italy. Thanks, Alessandro!

Instant fame is easy at Linux Journal. Just send us your useful Tech Tips to share with the Linux Community, and we'll send you a cool t-shirt for your efforts!

Please note: Tech Tips featured in this specific section of LinuxJournal.com are kindly brought to us by readers and are not necessarily tested by LinuxJournal.com editors.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

pgrep

oneguycoding's picture

I find pgrep to be really useful for identifying processes,

pgrep ssh
3161
3815
3957
4098
4168
4320
7089
9647
19727

and then you can do fun stuff like,

for p in $(pgrep ssh) ; do
ps -p $p -f --no-headers
done

and this eliminates the need to do any tweeks to avoid grepping grep.

:wq

How does this work? I type

Gordon J Milne's picture

How does this work?

I type "ps -ef | grep g[e]tty" and get this:

root      1878     1  0 Nov23 tty1     00:00:00 /sbin/getty 38400 tty1
root      1879     1  0 Nov23 tty2     00:00:00 /sbin/getty 38400 tty2
root      1880     1  0 Nov23 tty3     00:00:00 /sbin/getty 38400 tty3
root      1881     1  0 Nov23 tty4     00:00:00 /sbin/getty 38400 tty4
root      1882     1  0 Nov23 tty5     00:00:00 /sbin/getty 38400 tty5
root      1883     1  0 Nov23 tty6     00:00:00 /sbin/getty 38400 tty6

So it seems to work.

Now, if I type "ps -ef | grep getty >xxx" and then "cat xxx" I get this:

root      1878     1  0 Nov23 tty1     00:00:00 /sbin/getty 38400 tty1
root      1879     1  0 Nov23 tty2     00:00:00 /sbin/getty 38400 tty2
root      1880     1  0 Nov23 tty3     00:00:00 /sbin/getty 38400 tty3
root      1881     1  0 Nov23 tty4     00:00:00 /sbin/getty 38400 tty4
root      1882     1  0 Nov23 tty5     00:00:00 /sbin/getty 38400 tty5
root      1883     1  0 Nov23 tty6     00:00:00 /sbin/getty 38400 tty6
root     11504 11455  0 10:44 pts/0    00:00:00 grep getty

As you would expect, xxx contains the grep command. Now, I type "cat xxx | grep g[e]tty" and find that it gives me:

root      1878     1  0 Nov23 tty1     00:00:00 /sbin/getty 38400 tty1
root      1879     1  0 Nov23 tty2     00:00:00 /sbin/getty 38400 tty2
root      1880     1  0 Nov23 tty3     00:00:00 /sbin/getty 38400 tty3
root      1881     1  0 Nov23 tty4     00:00:00 /sbin/getty 38400 tty4
root      1882     1  0 Nov23 tty5     00:00:00 /sbin/getty 38400 tty5
root      1883     1  0 Nov23 tty6     00:00:00 /sbin/getty 38400 tty6
root     11504 11455  0 10:44 pts/0    00:00:00 grep getty

Why does the same trick not work?

Frankly, I cannot see how the square brackets trick works. Can you enlighten me please.

Dumb Me

gjmilne's picture

Sorry, I am dumb. I now see how the grep 'trick' works.