Cut pid from ps using cut command | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Cut pid from ps using cut command

UNIX for Dummies Questions & Answers


Tags
cut -d

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-11-2013
ran ber ran ber is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 11 March 2013, 6:01 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Cut pid from ps using cut command

hay
i am trying to get JUST the PID from the ps command.
my command line is:

ps -ef | grep "mintty" | cut -d' ' -f2
but i get an empty line. i assume that the delimiter is not just one space character, but can't figure out what should i do in order to do that.

i know i can use awk or cut -c1-4 (the pid is in characters 1-4), but i don't want to use them.

is it possible to achieve my goal using cut -f and a combination or -d or -s?

thanks
Sponsored Links
    #2  
Old 03-11-2013
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 25 September 2014, 5:44 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,422
Thanks: 8
Thanked 541 Times in 519 Posts
Tools take leading whitespace characters as one empty field, many empty fields or as leading space of the first field. You might use sed, so you can control field definition with regex. The ps output is also problematic as fields change form by value. Newer ps allow you to specify what fields you want with -o.
Sponsored Links
    #3  
Old 03-11-2013
ran ber ran ber is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 11 March 2013, 6:01 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
thanks, but i want to use only ps, grep and cut, is it eve possible the way i started?
    #4  
Old 03-11-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 26 September 2014, 6:37 PM EDT
Location: Galactic Empire
Posts: 3,385
Thanks: 234
Thanked 1,208 Times in 1,134 Posts
Try using awk instead:

Code:
ps -ef | awk '/mintty/&&!/awk/{print $2 } '

OR use pgrep

Code:
pgrep mintty

The Following User Says Thank You to Yoda For This Useful Post:
hanson44 (03-11-2013)
Sponsored Links
    #5  
Old 03-11-2013
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 973 Times in 789 Posts
This is a task tailor-made for pgrep. If you insist on using a ps pipeline, why? Is it a homework assignment? Is pgrep not available? If not, what OS are you using?

Regards,
Alister
Sponsored Links
    #6  
Old 03-11-2013
hanson44 hanson44 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 12 May 2013, 11:33 PM EDT
Posts: 858
Thanks: 18
Thanked 180 Times in 177 Posts
Doesn't make sense to say "I want to use only ps, grep and cut".

---------- Post updated at 04:23 PM ---------- Previous update was at 04:20 PM ----------

pgrep - I learned something new. Thanks

I had pidof written down in my notebook. That might work for you.
Sponsored Links
    #7  
Old 03-11-2013
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 973 Times in 789 Posts
pgrep is almost certainly more reliable than pidof. I am not absolutely certain, but I believe pidof is only found on Linux systems. pgrep (and pkill) are virtually everywhere.

Regards,
Alister
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
Cut Command error cut: Bad range dgmm Shell Programming and Scripting 2 05-22-2011 12:56 PM



All times are GMT -4. The time now is 12:23 AM.