arraysbashawketcpasswd

How to read awk from one file and compare it with awk from another file


The script I have is outputting $1 the most often logged users in the past $2 days

#var[$i]=$(awk -F: '{print $5}' /etc/passwd);
last -s -$2days | awk '{users[$1]++} END{for(i in users){print users[i], i}}' | sort -r | head -$1 | awk '{print $2 " -- " $1}';

so the output is following:

username -- (amount of logins)

But I'm trying to get an output with first and last name from /etc/passwd so it's:

first and last name, username -- (amount of logins)

But every idea I try to come up with doesn't seem to work for me. I tried looping the awk with /etc/passwd and then using -v in another awk or trying to get the output of 2nd awk into an array and then comparing it with /etc/passwd but I couldn't make either work. Does anyone have an idea how to handle this?


Solution

  • This should let you start:

    awk 'NR==FNR{name[$1]=$5;next} {u[$1]++}
        END{for(x in u)printf "%s, %s -- %d\n", name[x],x,u[x]} ' FS=":" /etc/passwd FS=" " <(last)