-
awk
hello
i'm writing a bash script and i want to use a command and filter it with awk,I do the following:
Code:
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
-
A couple of ways...
Code:
$ last | grep -v "wtmp" | awk -v u=$USER '$1==u {print $3}'
$ last | grep -v "wtmp" | awk '$1==ENVIRON["USER"] {print $3}'
-
Quote:
Originally Posted by
Salem
A couple of ways...
Code:
$ 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!
-
You should be able to get rid of the grep invocation something like this
Code:
$ 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
Code:
$ last | awk -v u=$usr '$1==u {print $3}'