cadvenom
asked on
need help combining grep and gawk commands
Hello, I am a bit of a newbie in need of some help with shell scripting...
I am trying to write a script using 'awk' and 'grep' that will go through all the results of the 'last' command, parsing out the user name, and matching it to the user's full name found in the /etc/passwd file.
any suggestions would be greatly appreciated.
So far I have something like this:
for uname in `last | grep $pattern | awk '{print $1}'`
do
gawk -F: '$uname {print $1, $5}' /etc/passwd
done
I am trying to write a script using 'awk' and 'grep' that will go through all the results of the 'last' command, parsing out the user name, and matching it to the user's full name found in the /etc/passwd file.
any suggestions would be greatly appreciated.
So far I have something like this:
for uname in `last | grep $pattern | awk '{print $1}'`
do
gawk -F: '$uname {print $1, $5}' /etc/passwd
done
ASKER
Thanks for the quick response.
I just thought of a solution that seems to work fine, if you're interested here it is:
for uname in `last | grep $pattern | awk '{print $1}'`
do
grep $uname /etc/passwd | awk -F: '{print $1, $5}'
done
I just thought of a solution that seems to work fine, if you're interested here it is:
for uname in `last | grep $pattern | awk '{print $1}'`
do
grep $uname /etc/passwd | awk -F: '{print $1, $5}'
done
ASKER
Thanks for the quick response.
I just thought of a solution that seems to work fine, if you're interested here it is:
for uname in `last | grep $pattern | awk '{print $1}'`
do
grep $uname /etc/passwd | awk -F: '{print $1, $5}'
done
I just thought of a solution that seems to work fine, if you're interested here it is:
for uname in `last | grep $pattern | awk '{print $1}'`
do
grep $uname /etc/passwd | awk -F: '{print $1, $5}'
done
How about:
last | while read uname JUNK
do
nawk -F: -v p=$pattern '{if($1~p)print $1,$5}' /etc/passwd
done
More efficient because you only use one awk process instead of two awks and two greps.
You might also want to do:
nawk -F: -v p=$pattern '{if($1~p)print $1,$5}' /etc/passwd | sort -u
to remove duplications.
Lots of ways to skin a cat
last | while read uname JUNK
do
nawk -F: -v p=$pattern '{if($1~p)print $1,$5}' /etc/passwd
done
More efficient because you only use one awk process instead of two awks and two greps.
You might also want to do:
nawk -F: -v p=$pattern '{if($1~p)print $1,$5}' /etc/passwd | sort -u
to remove duplications.
Lots of ways to skin a cat
Sorry, slight improvement to the nawk line:
nawk -F: -v p=$pattern '$1~p{print $1,$5}' /etc/passwd
nawk -F: -v p=$pattern '$1~p{print $1,$5}' /etc/passwd
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, didn't read the question.
nawk -F: -v u=$uname -v p=$pattern 'u~p&&u==$1{print $1,$5}' /etc/passwd
nawk -F: -v u=$uname -v p=$pattern 'u~p&&u==$1{print $1,$5}' /etc/passwd
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: glassd {http:#8223916}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
jmcg
EE Cleanup Volunteer
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: glassd {http:#8223916}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
jmcg
EE Cleanup Volunteer
for ix in $(last | cut -d' ' -f1);do grep $ix /etc/passwd | cut -d':' -f1,5;done;
ix being short for index, which I'm in the bad habit of using for pretty much any loop ;p
GL
Mike