How to get a list of group members?

Is there a command to get a list of group members? Something similar to the groups command, but instead of passing a username and returning groups, you pass it a groupname, and it returns members?

It is difficult to do it manually because the group membership information is split across two files, /etc/passwd for primary group, and /etc/group for secondary membership.

thank you kindly in advance.

akbar

Akbar,

Fill in 'xyz' with the group you want to search on, or put the line in a script with $1 instead:

grep xyz /etc/group | awk -F\: '{print $4}' | sed 's/\,/\n/g'

Here's a script (I call it lsgroup) that should do the trick:

awk -F: -v group=$1 '
        NR==FNR && $1==group {
                gid=$3
                for (i=1; i<=split($4,a,","); i++) print a
                next
        }
        NR!=FNR && $4==gid { print $1 }
' /etc/group /etc/passwd | sort -u

HPUX
logins -g groupname

This works under Solaris too!!!!

Exactly what I want... Thank's Methyl!

Kudos to Annihilannic for a very creative solution.

What a useful command, how come I never came across that one before :confused: Thanks!