From /etc/passwd, I need just the “user” part.
Example:
backup:x:34:34:backup:/var/backups:/bin/sh
has backup as the user.
To extract the user part I am using this now:
perl -pe 's/^(.*?):.*/\1/g' < /etc/passwd
which is a few characters less than my old method:
while read line; do echo ${line%%:*}; done < /etc/passwd
but it sounds like an overkill. It’s a lot of text to write.
A Google search gives me:
awk -F ':' '{print $1}' < /etc/passwd
which is equivalent, I suppose? Is it?
cmp <(perl -pe 's/^(.*?):.*/\1/g' < /etc/passwd) <(awk -F ':' '{print $1}' < /etc/passwd)
Is there a standard UNIX tool to do this or an easier method using perl? Or cut?
I am a beginner.
BTW, I tried my hand at Python as follows but I suck at it, there may be better ways to do this in Python :(
python -c 'print "\n".join([u[:u.find(":")] for u in open("/etc/passwd")])'
EDIT: actually, maybe more like this for Python:
python -c 'print "\n".join([u.split(":")[0] for u in open("/etc/passwd")])'
Hmm… still very verbose.
does nore-or-less what the
awksolution does.-ameans split each line of input into fields,-F:means that:is the field delimiter. And the-loutputs a newline for each line of input so you don’t have to sayprint "$F[0]\n".Since you requested it, the
cutsolution is even more straightforward:Meaning: split on
:, output the first field.