Question

TO U SESSIOII. a summary report for each user that shows the total number of hours and minutes a uter time that he or she has

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 ...

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 :)

Add a comment
Know the answer?
Add Answer to:
question 4: develop an awk program that reads /etc/passwd and prints the names of those users...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT