Results 1 to 7 of 7
What i want to do: list all users that have UID > 2000.
I know i can find information regarding the user in /etc/passwd and i know how to select ...
- 02-28-2008 #1Just Joined!
- Join Date
- Feb 2008
- Posts
- 2
Conditional statements
What i want to do: list all users that have UID > 2000.
I know i can find information regarding the user in /etc/passwd and i know how to select only the fields i'm interested in from that file using (cut -f1,3 -d
. But what composed command will solve my problem? How to i check if the 3'rd field returned by cat is greater that 2000 ?
- 02-28-2008 #2Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
You can use awk for the whole task:
awk '{FS=":"; if ($3 > 2000) print $1":"$3}' /etc/passwd
- 02-28-2008 #3Just Joined!
- Join Date
- Feb 2008
- Posts
- 2
Thanks, that worked! I'm curious though: are there any approaches that don't use awk?
- 02-28-2008 #4Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
- 02-28-2008 #5
I prefer to use cut:
awk is incredibly powerful (and may well be better suited to this problem), but cut is a less powerful alternative in many cases.Code:#!/bin/bash exec 3< /etc/passwd while read line <&3; do name=$(echo "$line" | cut -f1 -d:) id=$(echo "$line" | cut -f3 -d:) if [ "$id" -gt 2000 ]; then echo "$name" fi doneDISTRO=Arch
Registered Linux User #388732
- 02-29-2008 #6Linux User
- Join Date
- Aug 2006
- Posts
- 458
- 02-29-2008 #7Linux User
- Join Date
- Aug 2006
- Posts
- 458


Reply With Quote
