[Durham] interesting but expasperating problem sorting rows containing output from 'date' command [resend]

Martin Ward martin at gkc.org.uk
Thu Aug 15 16:54:11 UTC 2013


On Wednesday 14 Aug 2013 at 18:45, Dougie Nisbet <dougie at highmoor.co.uk> 
wrote:
> Running the command:
> 
>     cat unsorted.tst | sort -k 9n -Mk5 -k6n -k7n  | tac
> 
> 
> produces something very close to what I want, e.g:

The man page says:

`-k POS1[,POS2]'
`--key=POS1[,POS2]'
     Specify a sort field that consists of the part of the line between
     POS1 and POS2 (or the end of the line, if POS2 is omitted),
     _inclusive_.

     Each POS has the form `F[.C][OPTS]', where F is the number of the
     field to use, and C is the number of the first character from the
     beginning of the field.  Fields and character positions are
     numbered starting with 1; a character position of zero in POS2
     indicates the field's last character.  If `.C' is omitted from
     POS1, it defaults to 1 (the beginning of the field); if omitted
     from POS2, it defaults to 0 (the end of the field).  OPTS are
     ordering options, allowing individual keys to be sorted according
     to different rules; see below for details.  Keys can span multiple
     fields.

     Example:  To sort on the second field, use `--key=2,2' (`-k 2,2').
     See below for more notes on keys and more examples.

So your first option -k9n is using the data from field 9 (the year)
to the end of the line: which includes more digits on some lines.
Similarly for the other fields. To sort on *just* the ninth field
you want something like -k9n,9n

This seems to do what you want:

sort -k9n,9n -k5M,5M -k6n,6n -k7,7 

-- 
			Martin

STRL Reader in Software Engineering and Royal Society Industry Fellow
martin at gkc.org.uk  http://www.cse.dmu.ac.uk/~mward/  Erdos number: 4
G.K.Chesterton web site: http://www.cse.dmu.ac.uk/~mward/gkc/
Mirrors:  http://www.gkc.org.uk  and  http://www.gkc.org.uk/gkc



More information about the Durham mailing list