Grep/etc/passwd和/ etc/group列出用户所属的所有用户和每个组

Clu*_*Clu 1 linux bash shell scripting

我正在尝试grep /etc/passwd/etc/group列出所有用户和用户所属的每个组.

IE:

Jon Doe, root:randomgroup:randomgroup2:randomgroup3
Billy Bob, admin:apache:backups
Timmy Tim, root:www
Run Code Online (Sandbox Code Playgroud)

无论如何,这是我到目前为止,但我不完全确定如何让grep组和用户字段匹配.

cat /etc/passwd | cut -d: -f1,5
Run Code Online (Sandbox Code Playgroud)

这仅显示用户带有":"和全名

我该怎么做呢?

gva*_*kov 6

如果要求使用grep /etc/passwd并且/etc/group不是很难,请考虑以下解决方案:

for user in $(getent passwd | cut -d: -f1); do
    printf "%s: %s\n" $user "$(id -nG $user)"
done
Run Code Online (Sandbox Code Playgroud)

请注意@ JonathanLeffler的建议,因为这个问题并不像看起来那么简单.