Hi I have a command that produces the following:

/usr/ucb/ps -auxxx|awk '{print $3,$4,$2,$1,$11}'|grep -v 0.0
%CPU %MEM PID USER COMMAND
0.2 5.217405286920 1910 noaccess
0.1 0.1 180 root 0:43
0.1 0.1 686 root 0:41
0.1 0.1 26198 chris /usr/lib/ssh/sshd
0.1 0.1 26767 root /usr/ucb/ps
0.1 0.1 26215 root bash
0.1 0.1 26765 root sleep
0.1 0.1 26766 root sleep

I need it to make the first two colums to two decimal places i have tried for ages and cant seem to crack it any help?

Recommended Answers

All 4 Replies

You need to format your output using printf instead of using print.

yes I tired that but could only get the ouput to follow one line after the other and not stay in the columns can you give me the code please?

Try this and see if you can figure out:

echo "2.3 4.56789" | awk '{printf "%.2f %.2f\n", $1, $2}'
/usr/ucb/ps -auxxx | awk '{printf("%4s %4s %5s %4s %s\n", $3,$4,$2,$1,$11)}' | grep -v 0.0

probably does what you really want: line up the columns. If you truly want the numbers to be %5.2f format, you'll need to treat the column heading line differently.

Another option that might work:

ps -eo '%cpu %mem pid user comm' --sort user,pid | grep -v 0.0

if you just want certain data from ps() (and sorted on one or more fields).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.