Shell – Sorting more than one column

shell-scriptsorttext processing

I have a file which has content that looks like this:

19.58 1925 Alpha
20.40 1924 Otter
13.66 1920 Gold

I am trying to sort it "column by column" to output the following:

13.40 1920 Alpha
19.58 1924 Gold
20.66 1925 Otter

I have tried various combinations of code, e.g.:

cat files | sort -t. -k1,1n | sort -t " " -k2,2n -k3,3r k4,4n

but this doesn't output the wanted result. How could I achieve the sorting that I mentioned?

Best Answer

printf "%s.%s %s %s\n" $(paste <(awk -F'[. ]' '{print $1}' file | sort -n) <(awk -F'[. ]' '{print $2}' file | sort -n) <(awk -F'[. ]' '{print $3}' file | sort -n) <(awk -F'[. ]' '{print $4}' file | sort))

Output:

13.40 1920 Alpha
19.58 1924 Gold
20.66 1925 Otter
Related Question