Printing the user and group info

Hi All,

i want to collect all the users info whose id greater than 999 and print the groups information which they belong.

example :

for user in $(cut -d: -f1,3 /etc/passwd | egrep ':[0-9]{4}$' | cut -d: -f1); do groups $user; done
centos : centos adm wheel systemd-journal
balu : balu

Desired output :

user_name : "centos"
group_name: "centos"
user_name:"centos"
group_name:"adm"

likewise i want to print for all the users

i tried with awk syntax but it's not feasible as i need to dynamically pick the group names every time based on the ouput
Can someone please help me on this issue ?

Try

awk -F: '
$3 > 999        {FS = " "
                 ("groups " $1) | getline
                 for (i=3; i<=NF; i++) print $1, ":", $i
                 FS = ":"
                }
' /etc/passwd
mygroup=$(groups $user)
printf "User_name: \"$user\"\nGroup_name: \"%s\"\n" ${mygroup#*:}
 

You embed the username in the format string in printf. The ${mygroup#*:} strips everything to the left of the ":". Printf will keep repeating until all items in mygroup have been printed.

Andrew