Monday, 23 July 2018

GNU sort command to accurately sort dates on multiple fields

GNU sort command to accurately sort dates on multiple fields
Let's say you've got an ugly set of dates which are inconsistently padded with zeroes:
$ cat dates 
2/3/2011
02/02/2011
02/01/2011
12/3/2010
2/3/2011
01/03/2011
12/02/2010
01/02/2011
12/1/2010
1/1/2011
Using forward slash as the delimiter, we tell sort to sort on the third field first (year), then on the first field (month) and finally on the second field (day):
$ cat dates | sort -t/ -k 3n -k 1n -k 2n
12/1/2010
12/02/2010
12/3/2010
1/1/2011
01/02/2011
01/03/2011
02/01/2011
02/02/2011
2/3/2011
2/3/2011

0 comments:

Post a Comment