While both dlambin's and Sorpigal's answers are excellent and accomplish the job nicely, I just wanted to document my finding of how to use awk
to find the STAT column when changing between ps
output formats, instead of hard-coding it:
ps au | awk '{ if (NR==1) { for (i=1; i<=NF; i++) { if ($i=="STAT") stat=i }; print } else if ($stat~/^Z/) print}'
This will list all processes in ps
's "user-oriented format" and pipe into awk. On the header line (NR=record number == 1) it will walk all fields (up to NF=number of fields) and store the field number where the string "STAT" matches in the variable stat, then proceeding to print the line. For all other records (lines), it will check that column for a regex match of starting with a capital Z, only then printing the line.
Condensed version:
ps au|awk '{if(NR==1){for(i=1;i<=NF;i++){if($i=="STAT")stat=i};print}else if($stat~/^Z/)print}'