Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

How to extract the PID from 'ps -ef'

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-20-2006
nsinha nsinha is offline
Registered User
 
Join Date: Nov 2006
Last Activity: 28 January 2015, 3:52 AM EST
Posts: 14
Thanks: 8
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 Unix and Linux 11-20-2006
Yogesh Sawant's Unix or Linux Image
Yogesh Sawant Yogesh Sawant is offline Forum Advisor  
Full Time Dad
 
Join Date: Sep 2006
Last Activity: 16 April 2015, 11:33 AM EDT
Location: Rossem, Tazenda
Posts: 1,211
Thanks: 6
Thanked 18 Times in 17 Posts
do this:

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

replace XYZ withwhatever you have
Sponsored Links
    #3  
Old Unix and Linux 11-20-2006
nsinha nsinha is offline
Registered User
 
Join Date: Nov 2006
Last Activity: 28 January 2015, 3:52 AM EST
Posts: 14
Thanks: 8
Thanked 0 Times in 0 Posts
Hi Yogesh

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

Thanks
Nirmalya
    #4  
Old Unix and Linux 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 Unix and Linux 11-20-2006
aigles's Unix or Linux Image
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 1 December 2014, 9:03 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 62 Times in 58 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 Unix and Linux 11-20-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 9 April 2015, 4:19 AM EDT
Location: classification algos
Posts: 3,206
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 Unix and Linux 11-20-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 9 April 2015, 4:19 AM EDT
Location: classification algos
Posts: 3,206
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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 11:06 PM.