Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 689
  • Last Modified:

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
0
cadvenom
Asked:
cadvenom
1 Solution
 
edeyCommented:
mmm, looking at what you've got this is how I'd have done it:

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
0
 
cadvenomAuthor Commented:
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
0
 
cadvenomAuthor Commented:
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
glassdCommented:
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
0
 
glassdCommented:
Sorry, slight improvement to the nawk line:

nawk -F: -v p=$pattern '$1~p{print $1,$5}' /etc/passwd
0
 
glassdCommented:
Sorry, didn't read the question.

nawk -F: -v u=$uname -v p=$pattern 'u~p&&u==$1{print $1,$5}' /etc/passwd
0
 
glassdCommented:
Sorry, didn't read the question.

nawk -F: -v u=$uname -v p=$pattern 'u~p&&u==$1{print $1,$5}' /etc/passwd
0
 
jmcgOwnerCommented:
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
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now