12

How to write a script for linux which list all users from /etc/passwd and their UID

User1 uid=0001
User2 uid=0002
...

the script should use: grep, cut, id, for

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Tadeusz Majkowski
  • 612
  • 2
  • 8
  • 26

4 Answers4

34
awk -F: '$0=$1 " uid="$3' /etc/passwd

awk is easier in this case.

-F defines field separator as :

so you want is 1st and 3rd colums. so build the $0 to provide your output format.

this is very basic usage of powerful awk. you may want to read some tutorials if you faced this kind of problem often.

This time you got fish, if I were you, I am gonna do some research on how to fishing.

Kent
  • 189,393
  • 32
  • 233
  • 301
9

cut is nice for this:

cut -d: -f1 /etc/passwd

This means "cut, using : as the delimeter, everything except the first field from each line of the /etc/passwd file".

Will
  • 4,241
  • 4
  • 39
  • 48
4

I think best option is as that:
grep "/bin/bash" /etc/passwd | cut -d':' -f1

Sanjar Stone
  • 874
  • 7
  • 7
1
awk  -F':' '$3>999 {print $1 " uid: " $3}' /etc/passwd | column -t | grep -v nobody

Gives you all regular users (uid >= 1000) neatly ordered including the userid.

sjas
  • 18,644
  • 14
  • 87
  • 92