Skip to main content
Removed a typo which mercifully didn't cause any issues I could see. This should be more clear and more predicable.
Source Link
Kronos
  • 91
  • 1
  • 2
sortbyfield="rss"; fsep="-o "zzzzzz:::zzz%%"zzz%% -o"; ps ax o user:16 $fsep pid $fsep pcpu $fsep pmem $fsep vsz $fsep rss $fsep tty $fsep stat $fsep lstart $fsep time:16 $fsep cmd --sort -$sortbyfield | awk 'function setprefix(num){{n_suffix=1; while(num > 1000 && n_suffix < suffixes_len) {num /= 1024; n_suffix++;}; num=int(100*num)/100suffixes[n_suffix]}; return num} BEGIN{suffixes_len=split("kB MB GB TB PB EB ZB",suffixes);FS="zzz:::zzz%";} NR>1 {$5=setprefix($5);$6=setprefix($6); }{ printf "%-16s %6s %-5s %-5s %9s %9s %-8s %-8s %-25s %-18s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11;}' |head -n20 |cut -c -250 
sortbyfield="rss"; fsep="-o "zzzzzz:::zzz%%"zzz%% -o"; ps ax o user:16 $fsep pid $fsep pcpu $fsep pmem $fsep vsz $fsep rss $fsep tty $fsep stat $fsep lstart $fsep time:16 $fsep comm --sort -$sortbyfield | awk 'function setprefix(num){{n_suffix=1; while(num > 1000 && n_suffix < suffixes_len) {num /= 1024; n_suffix++;}; num=int(100*num)/100suffixes[n_suffix]}; return num} BEGIN{suffixes_len=split("kB MB GB TB PB EB ZB",suffixes);FS="zzz:::zzz%";} NR>1 {$5=setprefix($5);$6=setprefix($6); }{ printf "%-16s %6s %-5s %-5s %9s %9s %-8s %-8s %-25s %-18s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11;}' |head -n20 |cut -c -250 
sortbyfield="rss"; fsep="-o "zzz:::zzz%%" -o"; ps ax o user:16 $fsep pid $fsep pcpu $fsep pmem $fsep vsz $fsep rss $fsep tty $fsep stat $fsep lstart $fsep time:16 $fsep cmd --sort -$sortbyfield | awk 'function setprefix(num){{n_suffix=1; while(num > 1000 && n_suffix < suffixes_len) {num /= 1024; n_suffix++;}; num=int(100*num)/100suffixes[n_suffix]}; return num} BEGIN{suffixes_len=split("kB MB GB TB PB EB ZB",suffixes);FS="zzz:::zzz%";} NR>1 {$5=setprefix($5);$6=setprefix($6); }{ printf "%-16s %6s %-5s %-5s %9s %9s %-8s %-8s %-25s %-18s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11;}' |head -n20 |cut -c -250 
sortbyfield="rss"; fsep="-o "zzz:::zzz%%" -o"; ps ax o user:16 $fsep pid $fsep pcpu $fsep pmem $fsep vsz $fsep rss $fsep tty $fsep stat $fsep lstart $fsep time:16 $fsep comm --sort -$sortbyfield | awk 'function setprefix(num){{n_suffix=1; while(num > 1000 && n_suffix < suffixes_len) {num /= 1024; n_suffix++;}; num=int(100*num)/100suffixes[n_suffix]}; return num} BEGIN{suffixes_len=split("kB MB GB TB PB EB ZB",suffixes);FS="zzz:::zzz%";} NR>1 {$5=setprefix($5);$6=setprefix($6); }{ printf "%-16s %6s %-5s %-5s %9s %9s %-8s %-8s %-25s %-18s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11;}' |head -n20 |cut -c -250 
sortbyfield="rss"; fsep="-o zzz:::zzz%% -o"; ps ax o user:16 $fsep pid $fsep pcpu $fsep pmem $fsep vsz $fsep rss $fsep tty $fsep stat $fsep lstart $fsep time:16 $fsep cmd --sort -$sortbyfield | awk 'function setprefix(num){{n_suffix=1; while(num > 1000 && n_suffix < suffixes_len) {num /= 1024; n_suffix++;}; num=int(100*num)/100suffixes[n_suffix]}; return num} BEGIN{suffixes_len=split("kB MB GB TB PB EB ZB",suffixes);FS="zzz:::zzz%";} NR>1 {$5=setprefix($5);$6=setprefix($6); }{ printf "%-16s %6s %-5s %-5s %9s %9s %-8s %-8s %-25s %-18s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11;}' |head -n20 |cut -c -250 
sortbyfield="rss"; fsep="-o zzz:::zzz%% -o"; ps ax o user:16 $fsep pid $fsep pcpu $fsep pmem $fsep vsz $fsep rss $fsep tty $fsep stat $fsep lstart $fsep time:16 $fsep comm --sort -$sortbyfield | awk 'function setprefix(num){{n_suffix=1; while(num > 1000 && n_suffix < suffixes_len) {num /= 1024; n_suffix++;}; num=int(100*num)/100suffixes[n_suffix]}; return num} BEGIN{suffixes_len=split("kB MB GB TB PB EB ZB",suffixes);FS="zzz:::zzz%";} NR>1 {$5=setprefix($5);$6=setprefix($6); }{ printf "%-16s %6s %-5s %-5s %9s %9s %-8s %-8s %-25s %-18s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11;}' |head -n20 |cut -c -250 
added a more in-depth explanation about how the arbitrary field separator works and my reasoning for choosing the field separator I chose.
Source Link
Kronos
  • 91
  • 1
  • 2

Since I was processing the outputdiscovered an apparently undocumented trick while playing around with awk which can handle multi-character field separators, I used zzz:::zzz% as mythe field separator. To do this I have to passdescriptors located in the zzz:::zzz%%AIX FORMAT DESCRIPTORS tosub heading of the ps (notemanpage. It seams that it will include any characters before the extra % on each output line when you do something like %U for USER or %z for VSZ for example. I wondered what would happen if I tried %%. It showed a % on every line. OK, how about an arbitrary string without spaces? YES!! BUT apparently you HAVE to have %% in the string for things to work predictably. % doesn't get it. Beware that ps will try to parse everything after the % to see if its a valid field descriptor. Because of this, it is probably safest to keep it at the end) of the string like this FOOBARBIGSTRINGBLABLA%% to avoid issues. See my examples below

Now I just needed to come up with a string that has a relatively low chance of being a part of valid data - especially if I use cmd as a field. zzz:::zzz seems highly unlikely. lets just add the extra %% at the end to make it work - zzz:::zzz%% which shows up in the output as zzz:::zzz%.

I process the output with awk which can handle multi-character field separators, telling awk to look for zzz:::zzz% as the field separator.

EDIT 2: As requested, I added a more in-depth explanation about how the arbitrary field separator works and my reasoning for choosing the field separator I chose.

Since I was processing the output with awk which can handle multi-character field separators, I used zzz:::zzz% as my field separator. To do this I have to pass zzz:::zzz%% to ps (note the extra % at the end). See my examples below.

I discovered an apparently undocumented trick while playing around with the field descriptors located in the AIX FORMAT DESCRIPTORS sub heading of the ps manpage. It seams that it will include any characters before the % on each output line when you do something like %U for USER or %z for VSZ for example. I wondered what would happen if I tried %%. It showed a % on every line. OK, how about an arbitrary string without spaces? YES!! BUT apparently you HAVE to have %% in the string for things to work predictably. % doesn't get it. Beware that ps will try to parse everything after the % to see if its a valid field descriptor. Because of this, it is probably safest to keep it at the end of the string like this FOOBARBIGSTRINGBLABLA%% to avoid issues.

Now I just needed to come up with a string that has a relatively low chance of being a part of valid data - especially if I use cmd as a field. zzz:::zzz seems highly unlikely. lets just add the extra %% at the end to make it work - zzz:::zzz%% which shows up in the output as zzz:::zzz%.

I process the output with awk which can handle multi-character field separators, telling awk to look for zzz:::zzz% as the field separator.

EDIT 2: As requested, I added a more in-depth explanation about how the arbitrary field separator works and my reasoning for choosing the field separator I chose.

added a variable to call out the fact that you can also sort by any field
Source Link
Kronos
  • 91
  • 1
  • 2

Since I was processing the output with awk which can handle multi-character field separators, I used zzz:::zzz% as my field separator. To do this I have to pass zzz:::zzz%% to ps (note the extra % at the end). See my exampleexamples below:.

EDIT: I added a variable to call out the fact that you can also sort by any field using these examples. The names for the fields can be found in the manpage for ps under the heading STANDARD FORMAT SPECIFIERS

sortbyfield="rss"; fsep="-o "zzz:::zzz%%" -o"; ps ax o user:16 $fsep pid $fsep pcpu $fsep pmem $fsep vsz $fsep rss $fsep tty $fsep stat $fsep lstart $fsep time:16 $fsep cmd --sort -rss$sortbyfield | awk 'function setprefix(num){{n_suffix=1; while(num > 1000 && n_suffix < suffixes_len) {num /= 1024; n_suffix++;}; num=int(100*num)/100suffixes[n_suffix]}; return num} BEGIN{suffixes_len=split("kB MB GB TB PB EB ZB",suffixes);FS="zzz:::zzz%";} NR>1 {$5=setprefix($5);$6=setprefix($6); }{ printf "%-16s %6s %-5s %-5s %9s %9s %-8s %-8s %-25s %-18s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11;}'  |head -n20 |cut -c -250 
sortbyfield="rss"; fsep="-o "zzz:::zzz%%" -o"; ps ax o user:16 $fsep pid $fsep pcpu $fsep pmem $fsep vsz $fsep rss $fsep tty $fsep stat $fsep lstart $fsep time:16 $fsep comm --sort -rss$sortbyfield | awk 'function setprefix(num){{n_suffix=1; while(num > 1000 && n_suffix < suffixes_len) {num /= 1024; n_suffix++;}; num=int(100*num)/100suffixes[n_suffix]}; return num} BEGIN{suffixes_len=split("kB MB GB TB PB EB ZB",suffixes);FS="zzz:::zzz%";} NR>1 {$5=setprefix($5);$6=setprefix($6); }{ printf "%-16s %6s %-5s %-5s %9s %9s %-8s %-8s %-25s %-18s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11;}'  |head -n20 |cut -c -250 

Since I was processing the output with awk which can handle multi-character field separators, I used zzz:::zzz% as my field separator. To do this I have to pass zzz:::zzz%% to ps (note the extra % at the end). See my example below:

fsep="-o "zzz:::zzz%%" -o"; ps ax o user:16 $fsep pid $fsep pcpu $fsep pmem $fsep vsz $fsep rss $fsep tty $fsep stat $fsep lstart $fsep time:16 $fsep cmd --sort -rss | awk 'function setprefix(num){{n_suffix=1; while(num > 1000 && n_suffix < suffixes_len) {num /= 1024; n_suffix++;}; num=int(100*num)/100suffixes[n_suffix]}; return num} BEGIN{suffixes_len=split("kB MB GB TB PB EB ZB",suffixes);FS="zzz:::zzz%";} NR>1 {$5=setprefix($5);$6=setprefix($6); }{ printf "%-16s %6s %-5s %-5s %9s %9s %-8s %-8s %-25s %-18s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11;}'  |head -n20 |cut -c -250 
fsep="-o "zzz:::zzz%%" -o"; ps ax o user:16 $fsep pid $fsep pcpu $fsep pmem $fsep vsz $fsep rss $fsep tty $fsep stat $fsep lstart $fsep time:16 $fsep comm --sort -rss | awk 'function setprefix(num){{n_suffix=1; while(num > 1000 && n_suffix < suffixes_len) {num /= 1024; n_suffix++;}; num=int(100*num)/100suffixes[n_suffix]}; return num} BEGIN{suffixes_len=split("kB MB GB TB PB EB ZB",suffixes);FS="zzz:::zzz%";} NR>1 {$5=setprefix($5);$6=setprefix($6); }{ printf "%-16s %6s %-5s %-5s %9s %9s %-8s %-8s %-25s %-18s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11;}'  |head -n20 |cut -c -250 

Since I was processing the output with awk which can handle multi-character field separators, I used zzz:::zzz% as my field separator. To do this I have to pass zzz:::zzz%% to ps (note the extra % at the end). See my examples below.

EDIT: I added a variable to call out the fact that you can also sort by any field using these examples. The names for the fields can be found in the manpage for ps under the heading STANDARD FORMAT SPECIFIERS

sortbyfield="rss"; fsep="-o "zzz:::zzz%%" -o"; ps ax o user:16 $fsep pid $fsep pcpu $fsep pmem $fsep vsz $fsep rss $fsep tty $fsep stat $fsep lstart $fsep time:16 $fsep cmd --sort -$sortbyfield | awk 'function setprefix(num){{n_suffix=1; while(num > 1000 && n_suffix < suffixes_len) {num /= 1024; n_suffix++;}; num=int(100*num)/100suffixes[n_suffix]}; return num} BEGIN{suffixes_len=split("kB MB GB TB PB EB ZB",suffixes);FS="zzz:::zzz%";} NR>1 {$5=setprefix($5);$6=setprefix($6); }{ printf "%-16s %6s %-5s %-5s %9s %9s %-8s %-8s %-25s %-18s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11;}' |head -n20 |cut -c -250 
sortbyfield="rss"; fsep="-o "zzz:::zzz%%" -o"; ps ax o user:16 $fsep pid $fsep pcpu $fsep pmem $fsep vsz $fsep rss $fsep tty $fsep stat $fsep lstart $fsep time:16 $fsep comm --sort -$sortbyfield | awk 'function setprefix(num){{n_suffix=1; while(num > 1000 && n_suffix < suffixes_len) {num /= 1024; n_suffix++;}; num=int(100*num)/100suffixes[n_suffix]}; return num} BEGIN{suffixes_len=split("kB MB GB TB PB EB ZB",suffixes);FS="zzz:::zzz%";} NR>1 {$5=setprefix($5);$6=setprefix($6); }{ printf "%-16s %6s %-5s %-5s %9s %9s %-8s %-8s %-25s %-18s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11;}' |head -n20 |cut -c -250 
Source Link
Kronos
  • 91
  • 1
  • 2
Loading