Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with awk or else

Status
Not open for further replies.

waxwork

Programmer
Aug 9, 2004
6
0
0
FR
Hi,

I've got a big problem that makes me crazy. By using the command : ps -efo user,tty,pid,ppid,pcpu,cpu,etime,time,args

I would like to forma the result as below

Process User TTY PID PPID %CPU C START TIME Command
------- ---- --- --- ---- ---- - ----- ---- -------

I use awk for that and here is the code

ps -efo user,tty,pid,ppid,pcpu,cpu,etime,time,args | tail +2 | awk 'BEGIN {


fmt="%-40s %-10s %-9s %-9s %-9s %-9s %-9s %-9s %-9s\n"
printf fmt, "Command", "User","Terminal","PID","PPID","%CPU","C","START","Time"
trt=sprintf(fmt,"-----------------","--------","---------","---------","-------"
,"-------","-------","-------","-------")
printf trt
}
{ fmt="%-40s %-10s %-9s %-9s %-9s %-9s %-9s %-9s %-9s\n"
printf fmt,$9,$1,$2,$3,$4,$5,$6,$7,$8
}'

But because of the column "Command" format, which can include more than 1 argument with spaces, awk think there is more than one argument.
How can i get the entire argument from the 8th one? Or do you think about an alternative solution??

Thankx!!
 
hi ,

try something like
ps -efo user,tty,pid,ppid,pcpu,cpu,etime,time,args | tail +2 | {while read C U T P PP PC CPU ET TI
do

print $C $U $T etc ....
done
}
 
You may try something like this:
ps -efo user,tty,pid,ppid,pcpu,cpu,etime,time,args | awk '
BEGIN {
fmt="%-40s %-10s %-9s %-9s %-9s %-9s %-9s %-9s %-9s %s\n"
printf fmt,"Command","User","Terminal","PID","PPID","%CPU","C","START","Time","Args"
printf fmt,"-----------------","--------","---------","---------","-------","-------","-------","-------","-------",""
}
NR>1 {
a="";for(i=10;i<=NF;++i)a=a" "$i
printf fmt,$9,$1,$2,$3,$4,$5,$6,$7,$8,a
}'


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
It works fine. Thanks.

But i didn't understand your answer, can you explain me ??

Thankx
 
a="";for(i=10;i<=NF;++i)a=a" "$i
Concatenate all the remaining fields from the 10th into a variable named a.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top