4

I want to list the username, id and group on /etc/passwd using the following format: username uid gid

I have used the following:

cut -d: -f1,3,4 /etc/passwd

But it returns username:uid:gid. How can I format the command to remove the : or list without it, like this:

root 0 0
daemon 1 1
bin 2 2
...
5
  • show your expected output
    – phuclv
    May 19, 2022 at 2:43
  • @phuclv root 0 0 daemon 1 1 bin 2 2 and so on. With every user on a new line. May 19, 2022 at 2:46
  • 2
    Don't process /etc/passwd (or /etc/group) directly. Use getent passwd (or getent group) instead. /etc/passwd is only one of many possible sources of data for user account info. ditto for /etc/group. See the man pages for nss, getent, and nsswitch.conf. e.g. using @phuclv's awk example: getent passwd | awk -F: '{print $1, $3, $4}'
    – cas
    May 19, 2022 at 7:17
  • @cas Please consider writing an answer using getent instead of a comment. That way people can upvote it, and it can potentially be selected as the correct answer to achieve what the OP wants.
    – marcelm
    May 19, 2022 at 16:53
  • @marcelm phuclv's answer does a good job of answering the question (which is why i upvoted it). My comment wasn't an answer, it didn't answer the question (except for copying part of phuclv's answer as an example), it was just a comment that there are good reasons to use getent instead of reading /etc/passwd directly.
    – cas
    May 20, 2022 at 2:22

2 Answers 2

8

Depending on what output you want

The simplest way is to translate the delimiter to what you want with tr, sed or awk... For example

cut -d: -f1,3,4 /etc/passwd | tr ':' '\t'
cut -d: -f1,3,4 /etc/passwd | sed 's/:/ --- /g'
awk -F: '{ print $1, $3, $4}' /etc/passwd

If you want to format as table then use column

cut -d: -f1,3,4 /etc/passwd | column -t -s ':'
5

My version of cut (cut (GNU coreutils) 8.28) has a --output-delimiter argument:

cut -d: -f 1,3,4 --output-delimiter " " /etc/passwd

result:

root 0 0
daemon 1 1
bin 2 2
sys 3 3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .