1834456 Members
3308 Online
110067 Solutions
New Discussion

grep question ..

 
SOLVED
Go to solution
someone_4
Honored Contributor

grep question ..

I have what is probably a simple grep question:
we have a process call tgu_stats
Example 1. Command and output
#ps -ef|grep tgu*
root 1884 29789 0 15:52:42 pts/td 0:00 grep tgu_stats.log

Example 2. Command and output.
#ps -ef|grep tgu
root 1709 29789 1 15:51:48 pts/td 0:00 grep tgu
root 680 1 23 15:46:04 pts/td 0:04 tgu_stats

Why when I grep for tgu* I dont get back the tgu_stats proces? And when I grep for tgu I dont get back tgu_stats.log?

Thanks
Richard


9 REPLIES 9
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: grep question ..

Hi Richard:

You are confused about the way grep works. You don't need the '*' at all.

In example 1)
The tgu* was expanded by the shell to look for all files beginning with tgu so that your actual grep command was grep tgu_stats.log
and the only process it found was you grep
command itself.
In example 2) tgu_stats.log is a FILE not a PROCESS sops would not find it.

To do this correctly:
ps -ef | grep "tgu" | grep -v "grep"

The last grep gets rid of the grep process itself.

I hope that clears it up, Clay


#ps -ef|grep tgu*


root 1884 29789 0 15:52:42 pts/td 0:00 grep tgu_stats.log

If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: grep question ..

Richard,

Try with ps -ef|grep 'tgu*'

and see if you get any difference.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: grep question ..

And when you do a ps -ef|grep tgu*,
the shell tries to spawn tgu*. If your current directory has tgu_stats.log, then you won't get the correct output.

So, go to another directory where you don't have anyfile starting with tgu and run this command, you will get the correct output like what you get with ps -ef|grep 'tgu*'

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
linuxfan
Honored Contributor

Re: grep question ..

Hi Richard,

Another way to just grep your process is
ps -ef |grep [t]gu
This greps for any process starting with t followed by gu and thereby avoids using another grep (for grep -v) but then its a preference issue.

Ofcourse as far as your question is concerned about not getting the expected output when you use tgu*, that is because of the shell expansion.

-HTH
I am RU
They think they know but don't. At least I know I don't know - Socrates
Bill Hassell
Honored Contributor

Re: grep question ..

Although every basic sysadmin class teaches the ps piped to grep method, it is very unreliable, especially for scripting. grep is a simple string match so grep tgu matches every line with tgu anything. That means user logins, other programs and arguments on the command line. That's why you see the 'enhanced' version:

ps -ef | grep -v grep | grep -i

where something is what you are looking for. grep -v grep removes the grep process for tgu, but it also removes a process called grepping and a user name grepguru, which may not be desired.

So to find all processes that have a specific basename (the process name without any leading directories such as /usr/contrib/bin/tgu), use the PowerPill for ps called UNIX95:

UNIX95= ps -fC tgu_stats

No grep needed, and most important, it returns ONLY tgu_stats and not tgu_stats1, etc. Check out the difference between:

ps -ef | grep sh
UNIX95= ps -fC sh

Wow, what a difference. The second form (which activates the -C option in XPG4 mode) is really what we want.

Another feature of the PowerPill option is the ability to create a customized output format. SO if you don't need start-time (which varies in format), you can eliminate it. Consider this form:

UNIX95= ps -C sh -o ruser,pid,args

Now, very simple parsing can be done for the desired information. See man ps


Bill Hassell, sysadmin
Thierry Poels_1
Honored Contributor

Re: grep question ..

Hi,
I also prefer "ps -ef | grep [w]hatever" above "ps -ef | grep -v grep | grep whatever".

good one Ramesh!, sorry Bill ;)

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Sridhar Bhaskarla
Honored Contributor

Re: grep question ..

I completely agree with Bill. grep -v grep is a very fundamental way of doing things. For ex., I will run into problems if my process name is check_grep_oracle or my user id is grepmaster something like that where grep is an embedded string.

Also, Richard's problem will still exist if we use grep -v grep option. As long as he has tgu_stats.log or anything that starts with tgu in his current directory, he is never going to get tgu_stats in his grep tgu* command.

He will get correct output only when he does

1. ps -ef|grep 'tgu*'
2. Go to some other directory and execute grep 'tgu*' where there are no files that are starting with tgu*
3. The rocking way of using UNIX95

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Praveen Bezawada
Respected Contributor

Re: grep question ..

Hi
To remove the grep process being displayed so

ps -ef | grep [t]gu

with this we need not use grep -v grep

...BPK...
linuxfan
Honored Contributor

Re: grep question ..

Hi,

I like Bill's solution but the "Key" is that the process you are looking for should be the basename.

for example if you have a process called "tgu tgu_stats"
executing UNIX95= ps -fC tgu_stats will not return any output, where as UNIX95= ps -fC tgu, will return the desired output.

But my suggestion Richard is to avoid using * when grepping for anything and always enclose in ' ' to avoid shell misinterpretation.

-Regards
I am RU
They think they know but don't. At least I know I don't know - Socrates