question 4: develop an awk program that reads /etc/passwd and prints the names of those users having the same GID in the form GID name1 name2 ...
I found a similar solution on chegg but the solution didn't work. I can use bash, awk, grep, and sed to accomplish the result but no Perl.
Thanks in advance!
Develope an awk program that reads /etc/passwd and prints the names of those users having the same GID in the form GID name1 name2 ...
Here's the solution in pure bash:
#!/bin/bash
while IFS=: read name pw uid gid
remain; do
[ "${gid}" != "" ] && users[${gid}]+="${name} "
done < /etc/passwd
for gid in ${!users[@]}; do
echo "${gid}: ${users[${gid}]}"
done
Explanation:
We loop through each line of the /etc/passwd file, and then..
we check if gid is not null, and if not null..
we add the name of that user in users array at gid index
Once we loop through whole file, we have names of all users in the users array indexed at their corresponding gid
We then loop through users array and print user names at respective gid.
Sample output:
0: root sync shutdown halt operator
1: bin
2: daemon
4: adm
7: lp
Thumbs up if this helped :)
question 4: develop an awk program that reads /etc/passwd and prints the names of those users...