?
Solved

need help combining grep and gawk commands

Posted on 2003-03-25
9
Medium Priority
?
678 Views
Last Modified: 2011-04-14
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
Comment
Question by:cadvenom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 6

Expert Comment

by:edey
ID: 8208742
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
 

Author Comment

by:cadvenom
ID: 8214777
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
 

Author Comment

by:cadvenom
ID: 8214782
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:glassd
ID: 8223856
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
 
LVL 7

Expert Comment

by:glassd
ID: 8223867
Sorry, slight improvement to the nawk line:

nawk -F: -v p=$pattern '$1~p{print $1,$5}' /etc/passwd
0
 
LVL 7

Accepted Solution

by:
glassd earned 80 total points
ID: 8223916
Sorry, didn't read the question.

nawk -F: -v u=$uname -v p=$pattern 'u~p&&u==$1{print $1,$5}' /etc/passwd
0
 
LVL 7

Expert Comment

by:glassd
ID: 8223980
Sorry, didn't read the question.

nawk -F: -v u=$uname -v p=$pattern 'u~p&&u==$1{print $1,$5}' /etc/passwd
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10225154
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 Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question