Help with cmd while using ps

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

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,

 ps -e -o pid,uname,cmd
 returns below
 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,

 ps -e -o pid,uname,cmd | awk '{ print $3 }'  
 /sbin/mingetty
 /sbin/udevd
 -bash
 /sbin/dhclient

Expected value is

 /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
ps -e -o cmd

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:

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

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.

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= .

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.

ps -eo "%U,%t,%a" | awk -F"," 'NR > 1 {print $3}'

And if the arguments contain commas??

ps -eo "%U,%t,%a" | awk -F"," 'NR > 1 {print $3}'
...
ps -eo %U
...

:stuck_out_tongue:

If the arguments contain commas blame on you because you assumed. Cry, feel miserably for a while and learn to not do it again.
:stuck_out_tongue:

Thanks everyone for the reply.These where really helpful.