wiki:Scripts/GawkShowAllUsernamesInPsLists

Show Usernames in all ps Listings

Sometimes for some reason listing processes with ps -efly doesn't report every username. Instead it shows the user ID for some processes. This fragment will look-up the usernames and UIDs, put them in an associative array, and then use it to fixup the ps listing. Note the use of the stdin hyphen symbol at the end of the line to ensure that the piped input from ps is processed after gawk has built the array from /etc/passwd.

$ ps -efly | \
 gawk 'FILENAME == "/etc/passwd" {FS=":"; user[$3]= $1; } 
       FILENAME != "/etc/passwd" { FS=" "; $2 = $2 ~ /^[0-9]*$/ ? user[$2] : $2; print $0;}'
  /etc/passwd -