help with awk to create report | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

help with awk to create report

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-04-2005
ghazi ghazi is offline
Registered User
 
Join Date: Dec 2004
Last Activity: 3 July 2014, 11:47 AM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
help with awk to create report

Hi,

I am trying to create a report using the following syntax:

#!/bin/awk -f
#script name: users_report
BEGIN { FS=":" ; OFS="\t" ; print "User\tGID\tUser Name\tHome Dir\t"
{ print $1 , $3 , $5 , $6 }
END { print "\n End of Report \n" }

$> user_report /etc/passwd

the output of above code is as under:
User GID User Name Home Dir
jbond 500 James Bond /home/sales
pdavid 501 Pierre Davidd /home/Marketing
... so on
END of Report

now trying to change the GID number to group name from /etc/group first filed.

the output should look like this:

User GID User Name Home Dir
jbond SALES James Bond /home/sales
pdavid Market Pierre Davidd /home/Market
... so on
END of Report

Can someone help me complet this code with awk or with any other appropriate command syntax please.

Thanks in advance for your help.

Regards,
Ghazi
Sponsored Links
    #2  
Old 01-05-2005
bhargav's Avatar
bhargav bhargav is offline Forum Advisor  
Registered User
 
Join Date: Sep 2004
Last Activity: 26 July 2012, 3:38 PM EDT
Location: USA
Posts: 512
Thanks: 0
Thanked 2 Times in 2 Posts

Code:
awk 'FS=":" { OFS="\t" } 
BEGIN { print "User\tGID\tUser Name\tHome Dir" }
{ print $1,$3,$5,$6 }
END { print "\n End of the Report \n" }' /etc/passwd

Sponsored Links
    #3  
Old 01-05-2005
bhargav's Avatar
bhargav bhargav is offline Forum Advisor  
Registered User
 
Join Date: Sep 2004
Last Activity: 26 July 2012, 3:38 PM EDT
Location: USA
Posts: 512
Thanks: 0
Thanked 2 Times in 2 Posts

Code:
echo "User\tGID\tUser Name\tHome Dir"
cut -d":" -f 1,3,5,6 /etc/passwd | tr ":" "\t"
echo "\nEnd of the Report \n"

    #4  
Old 01-05-2005
druuna
Guest
 
Posts: n/a
12345

Last edited by druuna; 05-21-2009 at 09:10 AM..
Sponsored Links
    #5  
Old 01-05-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 24 Times in 23 Posts
I'll throw my script into the pot too...


Code:
#!/bin/ksh

printf "%-9s%-20s%-25s%-10s\n" "User" "Group" "Full Name" "Home"
printf "%-9s%-20s%-25s%-10s\n" "----" "-----" "---------" "----"

while read line
do
  gid=`echo $line | awk -F':' '{print $4}'`
  group=`grep ":$gid:" /etc/group | cut -d':' -f 1`
  echo "$line" | awk -vg=$group -vFS=':' '{ printf( "%-9s%-20s%-25s%-10s\n", $1, g, $5, $6 ) }'
done < /etc/passwd

echo "END OF REPORT"

Cheers
ZB
Sponsored Links
    #6  
Old 01-05-2005
ghazi ghazi is offline
Registered User
 
Join Date: Dec 2004
Last Activity: 3 July 2014, 11:47 AM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you all. It really helped. It is a wonderful forum.

With best regards,
Ghazi
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Create Report from shell julien_83 Shell Programming and Scripting 3 10-08-2010 08:27 AM
How to format or create a matrix report from file manas_ranjan Shell Programming and Scripting 3 12-24-2008 08:38 AM
using todays date to create a report using grep MBN Shell Programming and Scripting 3 11-02-2008 03:14 AM
create a report using shell moco Shell Programming and Scripting 10 04-12-2007 06:01 AM



All times are GMT -4. The time now is 12:14 AM.