|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
thanks, but i want to use only ps, grep and cut, is it eve possible the way i started?
|
|
#4
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Tags |
| cut -d |
| Thread Tools | Search this Thread |
| 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 |
|
|