Tuesday, April 27, 2010

Awk and Bash: Print Linux/Unix User Accounts

There are ways how to do this:
awk -F':' '{ print $1 "\t"  $3 }' /etc/passwd
That would  print the username and the UID. You can also add the sort command at the end like
awk -F':' '{ print $1 "\t"  $3 }' /etc/passwd | sort
and this will sort alphabetically.

To add filter in the result, you can
awk -F':' '$1 == "hachiko" { print $1 "\t"  $3 }' /etc/passwd | sort
where hachiko is a username you wanted to print with its UID.

Hope this helps!

No comments: