Sort numbers values - separated by a dot or any other separator character - Sort versions values in RHEL5

2k Views Asked by At

Linux RHEL5 machine

How can I sort the following input to get 1.0.0.1019 in latest variable? Tried -t, -k and -n but it didn't help or may be I'm missing something.

$ echo '1.0.0
1.0.0.1018
1.0.0.1019
1.0.0.1019
1.0.0.7' | sort -u 
2

There are 2 best solutions below

1
RavinderSingh13 On

Could you please try following and let me know if this helps(tested with GNU sort):

echo "1.0.0
1.0.0.1018
1.0.0.1019
1.0.0.1019
1.0.0.7" | sort --version-sort --field-separator=. --key=4 -r

Above will give 1019 in first place(latest one) in case you want it to last place then remove -r in above code please.

2
Ronald Macmaster On

sort -n -t. -k1,4 Sort the input numerically.
Fields are separated by '.'
Only use the first four fields, in that order.