Help with cmd while using ps


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with cmd while using ps
# 1  
Old 06-19-2015
Help with cmd while using ps

Hi i am new to shell scripting and any help is really appreciated.
my requirement is in,

Code:
ps -e -o pid,uname,cmd

how can i split and take only the cmd part from it.
I tried awk but the issue is when the cmd is returning a lengthy command which itself has some spaces it is truncating the value.for example,

Code:
 ps -e -o pid,uname,cmd

returns below

Code:
 2001 root     /sbin/mingetty /dev/tty6
 2002 root     /sbin/udevd -d
 2003 root     -bash
 2004 root     /sbin/dhclient -1 -q -lf /var/lib/dhclient/dhclient-eth0.leases -pf /var/run/dhclient-eth0.pid eth0

when using awk in ps to fetch only cmd it is truncating value as below,

Code:
 ps -e -o pid,uname,cmd | awk '{ print $3 }'

Code:
 /sbin/mingetty
 /sbin/udevd
 -bash
 /sbin/dhclient


Expected value is

Code:
 /sbin/mingetty /dev/tty6
 /sbin/udevd -d
 -bash
 /sbin/dhclient -1 -q -lf /var/lib/dhclient/dhclient-eth0.leases -pf /var/run/dhclient-eth0.pid eth0

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags for commands, data and terminal output. You see for yourself that it looks better and more readable.

Last edited by bakunin; 06-19-2015 at 12:38 PM..
# 2  
Old 06-19-2015
Code:
ps -e -o cmd

# 3  
Old 06-19-2015
Quote:
Originally Posted by Jojo90
Code:
ps -e -o pid,uname,cmd

how can i split and take only the cmd part from it.
You can of course specifiy only the "cmd"-part in the option-string of ps like Aia has already suggested. If you need all three values you can split these in the shell:

Code:
ps -e -o pid,uname,cmd |\
while read PID UNAME CMD ; do
     print - "PID=$PID , user=$UNAME , command=$CMD"
done

If you use a read command to fill several variables (three in this case) the input to fill them (one line from the output of ps) will be split along word boundaries, so the first "word" goes to the first variable, the second word to the second variable. If there are more "words" in the output than there are variables the last variable gets the whole rest of the line.

So, this works because the first two values are always single "words" (character groups surrounded by blanks), only the last one isn't. It would not work if there would be several multi-word values involved.

I hope this helps.

bakunin
# 4  
Old 06-19-2015
Tip1:
If you want this portable to Unix systems, stick to Posix.
The "Man Pages" at the top of the unix.com pages guide you to the Posix man page for ps.
Code:
ps -e -o pid,user,args |
while read PID USER ARGS ; do
  printf "%s\n" "PID=$PID , user=$USER , command='$ARGS'"
done

Also the ksh/zsh-only print was replaced by the portable printf.
Tip 2:
If you do not want a header line then use ps -e -o pid= -o user= -o args=.
# 5  
Old 06-19-2015
You know what they say. "Assume makes an ass of you and me". Nevertheless, if someone is, for some estrange reason, fixated in the exercise of getting the desired output, disregarding the portion that was included on purpose on first place, the output can be manipulated to be more convenient for manipulation with awk, in this case.

Example: You can tell ps to use a coma as a separator, and then use that separator easily with awk.
Code:
ps -eo "%U,%t,%a" | awk -F"," 'NR > 1 {print $3}'


Last edited by Aia; 06-19-2015 at 04:02 PM..
# 6  
Old 06-19-2015
And if the arguments contain commas??
Code:
ps -eo "%U,%t,%a" | awk -F"," 'NR > 1 {print $3}'
...
ps -eo %U
...

Smilie
# 7  
Old 06-19-2015
Quote:
Originally Posted by MadeInGermany
And if the arguments contain commas??
Code:
ps -eo "%U,%t,%a" | awk -F"," 'NR > 1 {print $3}'
...
ps -eo %U
...

Smilie
If the arguments contain commas blame on you because you assumed. Cry, feel miserably for a while and learn to not do it again.
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX Desktop Questions & Answers

Sftp cmd

What is the cmd line to connect sftp, specifing the port number and the shh key file. When I use the below cmd format, it gives the msg :node name or service name not known, but I know the connection works, because the automated sftp script works fine and I can log into the remote server ... (3 Replies)
Discussion started by: ITDev01
3 Replies

2. UNIX for Dummies Questions & Answers

Help with the find cmd

Hello, I'm having a trouble with the find cmd. I would like to find all the java versions on my systems. I have solaris 9 & 10 RHEL and SUSIE. java -version doesn't give all the versions on the server. So I am trying to use the find command to find them all find / -name java I would... (7 Replies)
Discussion started by: bitlord
7 Replies

3. Shell Programming and Scripting

Perl open(CMD, "cmd |"); buffering problem..

Hello, There's a third-party application's command that shows the application's status like "tail -f verybusy.log". When use the command, the output comes every 1-sec. but when it goes in a script below the output comes every 8-sec...What is the problem and how can I fix it? open(CMD,... (2 Replies)
Discussion started by: Shawn, Lee
2 Replies

4. Shell Programming and Scripting

Help with delete cmd

Is there any command to know ,how many files are deleted when rm *. cmd was excuted. I worte script which runs for 1 hours regularly and it is used to delete the .exe files but i need to know how many exe files are deleted regularly. Please let me know the cmd ---------- Post updated at... (3 Replies)
Discussion started by: kkalyan
3 Replies

5. Shell Programming and Scripting

Unix cmd prompt how to get old cmd run?

Hi, I am using SunOS I want to serch my previous command from unix prompt (like on AIX we can search by ESC -k) how to get in SunOs urgent help require. (10 Replies)
Discussion started by: RahulJoshi
10 Replies

6. AIX

Filesystems cmd

hi is that any command to find out which filesystems occupying more process in cpu utilisation (4 Replies)
Discussion started by: senmak
4 Replies

7. UNIX for Dummies Questions & Answers

sed cmd

write the sed command for swapping the first and 2nd (fields)words in the following file input file cse1 rama 1223 cse2 raju 2453 cse3 sita 3523 i tried with this $sed 's/ \(*\)/ \(*\)/ \2,\1' myfile1 but not getting th required... (4 Replies)
Discussion started by: sankar_vitam
4 Replies

8. UNIX for Dummies Questions & Answers

mv cmd

Hi All, How can I move only files to another destination using mv cmd in hp-ux. (2 Replies)
Discussion started by: mhbd
2 Replies

9. Programming

open cmd

if((LogFile=open(TempStr,O_CREAT|O_WRONLY|O_APPEND,0666))==-1) return(1); could someone explain me what the open() does here (2 Replies)
Discussion started by: bankpro
2 Replies

10. UNIX for Dummies Questions & Answers

man <cmd> >> cmd.txt

I've noticed most of my postings here are because of syntax errors. So I want to begin compiling a large txt file that contains all the "man <cmd>" of the commands I most have problems with. I ran a "man nawk >> nawk.txt" but it included a header/footer on each "page". Anyone know how I'd be... (6 Replies)
Discussion started by: yongho
6 Replies
Login or Register to Ask a Question