I have a big file with lines of the following format:
field1:field2:field3:field4:field5
I want to sort on field4
. However, field4
itself is 'subdivided' like this:
field41_field42_field43_field44
The command to sort field4
is
sort -t_ -k1,1d -k2,2g -k3,3g -k4,4g
So what I have to do is split each line by :
and then split the fourth field by _
and perform the specified sorting on it. Is there an easy way to do this?
A small subset of the data can be found here.
I don't like this myself - you could perform a Schwartzian transform by temporarily copying the fourth field to the beginning of the record, sort by it, and then remove it
awk -F':' -vOFS=':' '{print $4,$0}' file |
sort -k1,1d -k2,2n -k3,3n -k4,4n -t"_" |
cut -f2- -d":"