Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grep for a keyword

Status
Not open for further replies.

minus0

Programmer
Joined
Feb 20, 2003
Messages
73
Location
US
Hello all,

I am using a berkley ps to look at the processes running for a particular user, pipe the output to grep to look for a specific process - works fine but this is returning all the processes that match the word like a pattern matching but not the exact match. Let me give an example so that people don't get annoyed ;)
/usr/ucb/ps -auxww netuser | grep abc.ini

I expect to see the Process ID just for abc.ini and not some additional ones like XYZ-abc.ini, zzz-abc.ini

I tried grep -w for an exact word but that didn't help either. So, what I am missing here?
 
you could:
/usr/ucb/ps -auxww netuser | grep '[ ]abc.ini' which will look for the file with a space before it.
 
thanks for the reply but I am lost now - Why should I be using grep '[]abc.ini'? you said it will look for files with a space before it - there is no space before any of the files/processes running.

I want the search/grep to return me the process id for just
"abc.ini" - exactly like that but what is instead happening is I get the Pids for anything like abc.ini
 
/usr/ucb/ps -auxww netuser | grep ' abc.ini'

Hope This Help
PH.
 
Thanks PHV but that doesn't help - I already tried that before posting for help.
 
What is the output of this command ?
Code:
/usr/ucb/ps -auxww netuser | grep abc.ini

Hope This Help
PH.
 
I guess my explanation wasn't that good above - sorry about that. I am including the output here

$ /usr/ucb/ps -auxww netuser | grep abc.ini returns

netuser 3468 0.1 1.510010460552 pts/18 S 09:44:36 1:12 /usr/bin/../java/bin/../bin/sparc/native_threads/java -Xmx256M abc.ini
netuser 4911 0.0 0.1 968 696 pts/18 S 14:32:26 0:00 grep abc.ini
netuser 29371 0.0 0.55065619248 ? S Oct 30 0:00 /usr/bin/../java/bin/../bin/sparc/native_threads/java app1-abc.ini
netuser 2931 0.0 0.55065619248 ? S Oct 30 0:00 /usr/bin/../java/bin/../bin/sparc/native_threads/java app5-abc.ini
 
So the jad solution should be the good one:
Code:
/usr/ucb/ps -auxww netuser | grep '[ ]abc.ini'
or more restrictive:
Code:
/usr/ucb/ps -auxww netuser | grep ' abc\.ini$'

Hope This Help
PH.
 
I asked this before but never got a reply so I am asking you again - what is the space before the process for? I mean grep ' abc.ini' because if I include a space before the process then nothing is returned.

also when I do
/usr/ucb/ps -auxww netuser | grep '[ ]abc.ini'
I dont get anything in return. Is this specific to any OS/shell?
I am using solaris and ksh.
 
try
/usr/ucb/ps -auxww netuser | grep " [a]bc.ini"
 
You need a space before the abc.ini because grep really find lines like the pattern.
So it display line for app5-abc.ini because there is abc.ini in the line.
You have to search for something that is in the line for abc.ini and not in the line for app5-abc.ini.
So the above suggestions are to look for a space just before the word abc.ini
Code:
 grep ' abc.ini'
because there is no space before abc.ini in the line for app5-abc.ini.

Another problem is that you should still get the line for the grep process itself. You could get rid of it either by doing a 'reverse' grep to exclude line with grep:
Code:
grep ' abc.ini' | grep -v grep
or use one of the 'magic' patterns given above:
Code:
grep '[ ]abc.ini'

Well this should work. If not ... maybe you have an old grep utility.
Try using egrep (for 'expression grep') instead of grep.

"When all else has failed, read the manuals."

Denis
 
Some greps require -E for extended regular expressions, so try...

grep -E '[ ]abc.ini'
 
Denis,

I did take care of the process for the actual grep using a grep -v "grep".
coming to using a space/something that is unique to the process I am looking for, I know for sure that there will be at least 3 different processes with the name abc.ini, so one dirty, i ugess, option is to use a grep -v for all these other processes and get the PId for just the process I am interested in.

If i use a space like ' abc.ini', I dont get anything back.

thanks for your help
ps -
"When all else has failed, read the manuals." - I did check in the manuals but couldn't find anything related [sad]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top