Posts Tagged ‘cli’

How to exlude grep from a ps pipe result

Posted: October 3, 2012 in DIY, Linux, script
Tags: , , ,

How to exlude grep from a ps pipe result

Hello guys,

This is a short post of something I find very usefull if you have to do “ps -ef” often.

When you do a ” server#ps -ef | grep ‘something’ ” on any server you can se a line that also includes the grep process.

root@yourdiy.net:/# ps -ef | grep something
something      1084     1  0 Oct02 tty4     00:00:00 /sbin/getty 38400 tty4
something      1085     1  0 Oct02 tty5     00:00:00 /sbin/getty 38400 tty5
something      1086     1  0 Oct02 tty6     00:00:00 /sbin/getty 38400 tty6
something     20064 19246  0 09:44 pts/0    00:00:00 su
something     20109 20064  0 09:46 pts/0    00:01:21 bash
something     20748   883  0 17:28 ?        00:00:00 sleep 60
root     20750 20109  0 17:28 pts/0    00:00:00 grep something
root@yourdiy.net:/# 

You can see the  last line that includes the commands we just used, to avoid this you can simply use a negative form of grep.

 

root@yourdiy.net:/# ps -ef | grep something | grep -v "grep something"
something      1084     1  0 Oct02 tty4     00:00:00 /sbin/getty 38400 tty4
something      1085     1  0 Oct02 tty5     00:00:00 /sbin/getty 38400 tty5
something      1086     1  0 Oct02 tty6     00:00:00 /sbin/getty 38400 tty6
something     20064 19246  0 09:44 pts/0    00:00:00 su
something     20109 20064  0 09:46 pts/0    00:01:21 bash
something     20748   883  0 17:28 ?        00:00:00 sleep 60
root@yourdiy.net:/#

 

Like this you can see that the grep process is not longer listed, this is very usefull if you are using the output in a script.
I hope this little info help you.

Regards