Develop an awk program that reads /etc/passwd and prints the names of those user
ID: 3633289 • Letter: D
Question
Develop an awk program that reads /etc/passwd and prints the names of those users having the same GID in the form (GID name1 name 2)
I believe the input needs to be sorted and then you would just go down the list but I cant seem to get it to work in an awk file ..
Explanation / Answer
File gbyg.awk: BEGIN { FS = ":"; } # Set the field separator to a colon # Create an array entry for each group # Each element is a space-delmited list of users in that group { # If first user in the group, simoly set the corresponding array element equal to the userid if (groups[$4] == "") { groups[$4] = $1; } # If element already has a user assigned, add subsequent userid preceded by space else { groups[$4] = sprintf("%s %s",groups[$4],$1); } } END{ # For each group, print the output line for(group in groups) { # Test if more than one user in group (at least one space in element) # Comment out following line if you want to print groups with single user if (index(groups[group]," ") > 0) { # If multiple users in group, print print group, groups[group]; } # Comment out this line if you want to print groups with single user } } Output: $ awk -f gbyg.awk passwd.txt 5 uucp nuucp 20 arushkin trsmith 4294967294 nobody lpd Does the input data need to be sorted? No, the input does not need to be sorted (as can be seen above). Note: the problem did not ask for the output to be sorted by GID or UID.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.