The UNIX and Linux Forums  
Hello and Welcome from to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
column extract help cvm Shell Programming and Scripting 1 04-24-2008 05:19 PM
extract using sed/awk - need help? Please!! gzs553 Shell Programming and Scripting 5 10-08-2007 05:54 PM
Please help! Sed extract a pattern zhen Shell Programming and Scripting 11 09-18-2006 01:36 PM
tar. I can't extract volleyboy UNIX for Dummies Questions & Answers 1 02-17-2006 11:54 AM
extract from TAR big123456 UNIX for Advanced & Expert Users 8 07-28-2005 04:00 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-20-2006
nsinha nsinha is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 5
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
  #2 (permalink)  
Old 11-20-2006
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
do this:
Code:
ps -ef | grep XYZ | awk '{ print $2 }'
replace XYZ withwhatever you have
  #3 (permalink)  
Old 11-20-2006
nsinha nsinha is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 5
Hi Yogesh

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

Thanks
Nirmalya
  #4 (permalink)  
Old 11-20-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,953
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 }'
  #5 (permalink)  
Old 11-20-2006
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
Quote:
Originally Posted by matrixmadhan
how about this,

Code:
ps -ef | awk ' /XYZ/ { print $2 }'
better, that reduces one pipe
  #6 (permalink)  
Old 11-20-2006
Deepa Deepa is offline
Registered User
  
 

Join Date: Apr 2002
Posts: 53
tr command should help you to squeeze characters.
Try tr -s option.
  #7 (permalink)  
Old 11-20-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,953
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:58 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0