PDA

View Full Version : awk



quo
05-03-2012, 02:38 AM
hello

i'm writing a bash script and i want to use a command and filter it with awk,I do the following:


last | grep -v "wtmp" | awk '{print $3}'

but i want awk to print column 3 only if column 1 (which is the username) matches the variable $usr
Is there a way to do this comparison in awk?

thanks

Salem
05-03-2012, 03:24 AM
A couple of ways...


$ last | grep -v "wtmp" | awk -v u=$USER '$1==u {print $3}'
$ last | grep -v "wtmp" | awk '$1==ENVIRON["USER"] {print $3}'

quo
05-03-2012, 06:24 AM
A couple of ways...


$ last | grep -v "wtmp" | awk -v u=$USER '$1==u {print $3}'
$ last | grep -v "wtmp" | awk '$1==ENVIRON["USER"] {print $3}'


thanks a lot!

oogabooga
05-03-2012, 02:15 PM
You should be able to get rid of the grep invocation something like this


$ last | awk -v u=$usr '!/wtmp/ && $1==u {print $3}'

I don't know what column "wtmp" occurs in, but if it's in the user name column and $usr is not "wtmp", then just


$ last | awk -v u=$usr '$1==u {print $3}'