Brilliant. Seems like I'll have to take a look at awk. Thanks.
EDIT: OK, seems like you can make it with grep also ... but what's the role of UNIX95 variable?
For my current level, I'll just combine awk and filter out defunct zombies with grep.
---------- Post updated at 03:58 PM ---------- Previous update was at 03:39 PM ----------
Wait ... after awk ( ps -eo pid,comm,%cpu | awk '{OFS="|"; print $1,$2,$3}' ), how do I sort according to $3 field?
If I cut the 3rd field, I lose the PIDs.
---------- Post updated at 04:04 PM ---------- Previous update was at 03:58 PM ----------
You can sort on any field, or sort defaults to starting at the left.
So, you an keep in the current layout of three (or more/less) fields, and then work with the various sort options. You would specify the a -t option noting the '|' as delimiter. Check into the -n to specify numerics.
Or, awk can print in a different order. You could:
Note:
I can see that you are experimenting, and that is good. Where I am right now I am having difficulty recreating your ps command, thus the suggestions are all from memory.
Sorry, but I don't believe that this line is syntactically correct (because of the "%" character) - but I could be wrong.
What Operating System and version you you have? There is much variation in the "ps" command.
Assuming that the syntax is correct on your system (which I do not believe), the "-o" parameter to "ps" allows you to display the parameters in any order.
We don't need "awk" to change the order of the fields. I note that "purdym" knows this.
ps -eo %cpu,pid,comm | sort -nr | head -10
My version of the HP-UX "Top 10 CPU users" includes:
The "UNIX95" variable is specific to HP-UX and causes certain commands to behave with Berkeley unix syntax rather than Unix System V syntax. The manual suggests that the default "ps" syntax is "Posix" but that version is inferior to the Berkeley version.