First some context: This is the format of an /etc/passwd file (rough example)...
root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin
Now then, can you sort that (numerically, descending) by UID (3rd field) and do it with less code? Show me how! I bet there's a way to optimize-out that split below...
cat /etc/passwd | perl -e 'print join ":", @$_ for sort { $b->[2] <=> +$a->[2] } map { [ split /:/, $_ ] } <>'
Granted, you can use the code below also, but it doesn't port to a certain flavor of Unix I have to work with more than I'd like... The Perl code works on all the platforms without changes...
cat /etc/passwd | sort -t : -k3 -nr # the above method works great with GNU /bin/sort!
In reply to Golf: reverse sort /etc/passwd by UID by Tommy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |