How to extract the PID from 'ps -ef' | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to extract the PID from 'ps -ef'

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-20-2006
nsinha nsinha is offline
Registered User
 
Join Date: Nov 2006
Last Activity: 4 April 2014, 8:13 AM EDT
Posts: 12
Thanks: 4
Thanked 0 Times in 0 Posts
How to extract the PID from 'ps -ef'

Hi

I am writing a shell script in ksh where I have to grep for a process name , say XYZ from "ps -ef" and then extract it's PID.

ps -ef | grep XYZ gives -
" int 7738 25734 1 02:00:49 pts/tc 0:00 grep XYZ"

I am thinking of replacing one or more occurrences of the space with pipe "|", so the output will be -
|int|7738|25734|1|02:00:49|pts/tc|0:00|grep|XYZ

Then I want to use the "cut -d"|" -f3 which will give 7738, the PID.

But I am not able to replace one or more occurrences of the space with a single pipe. I have tried in many many different ways, but I am in vain.

Can you please show me the way how it can be done ?

Thanking you in advance ...
Nirmalya
Sponsored Links
    #2  
Old 11-20-2006
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Advisor  
Full Time Dad
 
Join Date: Sep 2006
Last Activity: 4 April 2014, 3:58 AM EDT
Location: Rossem, Tazenda
Posts: 1,211
Thanks: 6
Thanked 17 Times in 16 Posts
do this:

Code:
ps -ef | grep XYZ | awk '{ print $2 }'

replace XYZ withwhatever you have
Sponsored Links
    #3  
Old 11-20-2006
nsinha nsinha is offline
Registered User
 
Join Date: Nov 2006
Last Activity: 4 April 2014, 8:13 AM EDT
Posts: 12
Thanks: 4
Thanked 0 Times in 0 Posts
Hi Yogesh

Thanks a lot man. I should also try 'awk' - my mistake.

Thanks
Nirmalya
    #4  
Old 11-20-2006
Deepa Deepa is offline
Registered User
 
Join Date: Apr 2002
Last Activity: 4 October 2007, 12:33 AM EDT
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
tr command should help you to squeeze characters.
Try tr -s option.
Sponsored Links
    #5  
Old 11-20-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 20 January 2014, 5:51 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 61 Times in 57 Posts
Quote:
Originally Posted by nsinha
Hi

I am writing a shell script in ksh where I have to grep for a process name , say XYZ from "ps -ef" and then extract it's PID.

ps -ef | grep XYZ gives -
" int 7738 25734 1 02:00:49 pts/tc 0:00 grep XYZ"

I am thinking of replacing one or more occurrences of the space with pipe "|", so the output will be -
|int|7738|25734|1|02:00:49|pts/tc|0:00|grep|XYZ

Then I want to use the "cut -d"|" -f3 which will give 7738, the PID.

But I am not able to replace one or more occurrences of the space with a single pipe. I have tried in many many different ways, but I am in vain.

Can you please show me the way how it can be done ?

Thanking you in advance ...
Nirmalya
To replace spaces with pipe :

Code:
ps -ef | grep XYZ | sed 's/  */|/g'

I suggest you to use awk solution posted by Yogesh Sawant.

Your command will always returns the grep process, to avoid that you can use the following grep syntax :
Code:
ps -ef | grep [X]YZ


Jean-Pierre.
Sponsored Links
    #6  
Old 11-20-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 11 April 2014, 1:02 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Quote:
Originally Posted by Deepa
tr command should help you to squeeze characters.
Try tr -s option.
Am not sure, how would suppressing characters and that too which are repeated would prove to be helpful? Could you please explain that
Sponsored Links
    #7  
Old 11-20-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 11 April 2014, 1:02 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Quote:
Originally Posted by Yogesh Sawant
do this:

Code:
ps -ef | grep XYZ | awk '{ print $2 }'

replace XYZ withwhatever you have

how about this,


Code:
ps -ef | awk ' /XYZ/ { print $2 }'

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extract a value from log dreamgirl314 Shell Programming and Scripting 7 06-25-2011 10:22 AM
SED extract XML value ArterialTool Shell Programming and Scripting 13 08-27-2009 12:18 PM
How to Extract Harikrishna Solaris 3 06-20-2008 03:43 AM
extract using sed/awk - need help? Please!! gzs553 Shell Programming and Scripting 5 10-08-2007 04:54 PM
tar. I can't extract volleyboy UNIX for Dummies Questions & Answers 1 02-17-2006 10:54 AM



All times are GMT -4. The time now is 02:39 AM.