DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Substitution For 'ps -aux | Grep [P]ROCESS'

09.22.2006
| 20555 views |
  • submit to reddit
        as we know, invoking grep after ps with first letter of the PROCESS enclosed in brackets [] (like this: ps -aux | grep [P]ROCESS), excludes grep PROCESS from the output. so, instead of typing those brackets manualy every time, we may use the code below:
FIRST=`echo $1 | sed -e 's/^\(.\).*/\1/'`
REST=`echo $1 | sed -e 's/^.\(.*\)/\1/'`
ps -aux | grep "[$FIRST]$REST"

you may use it as separate shell-script -- like this:
#!/bin/sh
FIRST=`echo $1 | sed -e 's/^\(.\).*/\1/'`
REST=`echo $1 | sed -e 's/^.\(.*\)/\1/'`
ps -aux | grep -v "full/path/to/your/script" | grep "[$FIRST]$REST"

or just include it in your .bashrc or similar -- like this:
function psg
{
FIRST=`echo $1 | sed -e 's/^\(.\).*/\1/'`
REST=`echo $1 | sed -e 's/^.\(.*\)/\1/'`
ps -aux | grep "[$FIRST]$REST"
}
    

Comments

Snippets Manager replied on Tue, 2010/04/13 - 6:21am

RTFM ps -C for what??!

Snippets Manager replied on Mon, 2010/06/21 - 4:37am

BASH has internal functions for string manipulation. You can reduce the function code to this:

psg () 
{
ps aux | grep "[${1:0:1}]${1:1}"
}