Unix sort for 2 fields numerical order

I need to sort some data using unix sort, but I can’t pinpoint the correct syntax, the data looks like

3.9.1 Step 10:
3.9.1 Step 20:
3.8.10 Step 20:
3.10.2 Step 10:
3.8.4 Step 90:
3.8.4 Step 100:
3.8.4 Step 10:

I want to sort it using first the main number, then the step number, for example. data sorted above will look.

3.8.4 Step 10:
3.8.4 Step 90:
3.8.4 Step 100:
3.8.10 Step 20:
3.9.1 Step 10:
3.9.1 Step 20:
3.10.2 Step 10:

I found a way to sort by the first number on this site:

sort -t. -k 1,1n -k 2,2n -k 3,3n

but I try my best to sort the step number by the 3rd column without breaking the first view

+5
source share
4 answers

Unix sort ( " ", JP Linderman, AT & T Bell Labs Tech Journal, Oct 1984), , , , AFAICT ( , , , ). , , Unix sort ( , , , , "" - ). , , danfuzz; , . , , .

, , (, Control-A) .

sed 's/^\([^.]*\)[.]\([^.]*\)[.]\([^ ]*\) Step \([0-9]*\):.*/\1^A\2^A\3^A\4^A&/' file |
sort -t'^A' -k1,1n -k2,2n -k3,3n -k4,4n |
sed 's/^.*^A//'

. 4 ( ^A , Control-A), . , sed Control-A, .

+2

Step : sort, ? , , :

cat your-file.txt \
    | sed -e 's/ Step \(.*\):$/.\1/g' \
    | sort -t. -k1,1n -k2,2n -k3,3n -k4,4n \
    | sed -e 's/\(.*\)\.\(.*\)$/\1 Step \2:/g'

( cat . , sed.)

+2

:

 sort -k3,3n file | sort -nst. -k1,1 -k2,2 -k3,3

:

 sort -nt. -k1,1 -k2,2 -k3,3 -k3.7 file

:

  • sort -k3,3n
  • sort -nst. -k1,1 -k2,2 -k3,3 ,

, 100.

, :

sed 's/ /./2' file | sort -nt. -k1,1 -k2,2 -k3,3 -k4,4 | sed 's/\./ /3'
+2

:

sed 's/Step /Step./' data|sort -t. -n -k1,1 -k2,2 -k3,3 -k4|sed 's/Step./Step /'

:

3.8.4 Step 10:
3.8.4 Step 90:
3.8.4 Step 100:
3.8.10 Step 20:
3.9.1 Step 10:
3.9.1 Step 20:
3.10.2 Step 10:

A task of this type is that sort fields are defined both '.'(for version numbers) and standard spaces (for step numbers). You cannot specify multiple / different field separators for the same sort command. Combining several varieties with different field separators did not give the correct output.

This solution works by replacing the blank space after the field Steptemporarily '.'so that all sort fields can be separated by the same character ( '.'). After the sorting is completed, '.'it will again be replaced by an empty one.

+1
source

All Articles