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


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



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

12345

I'll throw my script into the pot too...

#!/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

Thank you all. It really helped. It is a wonderful forum.

With best regards,
Ghazi
:slight_smile: