sort
sort is a command to sort lines of data.
GNU Syntax Examples
In OS X, this is gsort
.
Avoid leading space caveat
There is a big caveat to using GNU sort with data that has inconsistent whitespace.
$ cat test-data.txt
foo 5
bar 2
baz 9
$ gsort -k2 --debug test-data.txt
gsort: text ordering performed using ‘en_US.UTF-8’ sorting rules
gsort: leading blanks are significant in key 1; consider also specifying 'b'
baz 9
__________
_____________
foo 5
______
_________
bar 2
___
______
As the debug text indicates, using -b
can avoid this, though I'm not sure why this isn't the default behavior:
$ gsort -b -k2 --debug test-data.txt
gsort: text ordering performed using ‘en_US.UTF-8’ sorting rules
bar 2
_
______
foo 5
_
_________
baz 9
_
_____________
Randomly sort a file in place
By giving the -o
the same output file as the input file we can shuffle in-place without errors. Trying this same thing using a pipe or a redirect will usually cause an empty file.
Beware that this will put duplicate lines right next to each other. If you need better file content shuffling use shuf
.
sort -o foo -R foo
Sort by multiple fields with different sort requirements
When sorting by multiple fields, it's important to specify the start and end of where you want the sort to occur. If you do not do this, you may get too short of a comparison, or too long of a comparison. Check the output of --debug
if you don't get the right sort order.
# -k defines the sort key as starting position, sort style, ending position
# -r is included in the second key to reverse numeric sort
gsort -k1d,1 -k2nr,2
Another practical example of this is sorting authorized_keys
files by their comment, putting commented keys at the bottom, which keeps the columns nicely aligned. For instance, if you have this file:
#ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBDhB9kjaireRsJgPASR2jJqU0o8UvIVIPunKNQmS+mw user@a-key-we-want-to-manually-enable
#ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeUlnQ8TVgYkI1/DvPMhHJoujYarUvdBx3/BA1mlZLs another-user@some-other-commented-key
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMWu8gF1qT19FtikCMoIBnmEJH1nKyrcC/pRCnvWzoSa bastion-01
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICY9ScGyTpXOLnYnUfqGDfdwMf4kRIPey1xvPRJ8CsAX root@some-old-box
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFSoe1Ho3PjOrS4Hz+B+ILHh40Xi2kbN2f7qb2tNKb1d admin@some-other-box
Normal sort authorized_keys
would put the comments at the top, and not sort by the third column, which is the human readable comment of the keys. A better view would be to reverse sort the first column so the comments are at the bottom, then sort by the third column so it's easy to glance through:
$ gsort -b -k1,1r -k3 authorized_keys
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFSoe1Ho3PjOrS4Hz+B+ILHh40Xi2kbN2f7qb2tNKb1d admin@some-other-box
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMWu8gF1qT19FtikCMoIBnmEJH1nKyrcC/pRCnvWzoSa bastion-01
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICY9ScGyTpXOLnYnUfqGDfdwMf4kRIPey1xvPRJ8CsAX root@some-old-box
#ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeUlnQ8TVgYkI1/DvPMhHJoujYarUvdBx3/BA1mlZLs another-user@some-other-commented-key
#ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBDhB9kjaireRsJgPASR2jJqU0o8UvIVIPunKNQmS+mw user@a-key-we-want-to-manually-enable
Sort IP Addresses by first octet then last octet, showing which fields are sorted
ip neigh show | sort -k1,1n -k4,4n -t. --debug
Console example:
$ ip neigh show | sort -k1,1n -k4,4n -t. --debug
sort: using ‘en_US.UTF-8’ sorting rules
10.0.2.2 dev eth0 lladdr 52:54:00:12:35:02 REACHABLE
__
_
____________________________________________________
10.0.2.3 dev eth0 lladdr 52:54:00:12:35:03 STALE
__
_
________________________________________________
192.16.35.10 dev eth1 lladdr 08:00:27:7a:50:42 STALE
___
__
____________________________________________________
192.16.35.11 dev eth1 lladdr 08:00:27:56:64:2f STALE
___
__
____________________________________________________
BSD Syntax Examples
GNU sort and BSD sort behave differently, which is mostly lame.
Sort by the third column
sort -k 3 filename
Sort dates by the day
This example shows how to sort dates in ISO Year format by date. (EG: 2017-01-19). Assumes use of bash
4 to generate the example dates.
## -n for numeric sort
## -k3 for column 3
## -t- to use - as a column delimiter
for X in {2016..2017}-{01..12..03}-{01..19..06} ; do echo ${X} ; done |
sort -n -k3 -t-
Sort the /etc/passwd by UID
Also works on /etc/group file and GID
sort -n -t: -k 3 /etc/passwd