I have a text files with a list of users formatted like:


Calling the command groups gives the primary group name for that user. ex:

bash$ groups name1
name1: group_Name

I want to get the groups of all the users of the textfile and put this list of groups into a new text file.

Does anyone know a command for this? Maybe a script the iterates the first file with the names and calls groups on each name then stores the output in a new file. I just need a little help with the specifics.


Read the file line by line, call groups for each item:

while read name ; do groups "$name" ; done < list.txt > with_groups.txt

Loops can often be replaced by xargs:

xargs groups < list.txt > with_groups.txt
| improve this answer | |

How about something like this:

while read ID
    groups $ID
done < input.file |
awk '{print $3}' | sort | uniq > list_of_groups

That gets you a lexically-sorted unique list of group names.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.