Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort negative decimal values

Dealing with the processing of the log in the following format:

# the system name: dG
MD_cluster10_1_300K: -34.48
MD_cluster11_1_300K: -40.31
MD_cluster1_1_300K: -40.23
MD_cluster2_1_300K: -37.86
MD_cluster3_1_300K: -25.65
MD_cluster5_1_300K: -11.91
MD_cluster6_1_300K: -0.50
MD_cluster8_1_300K: -16.62
MD_cluster9_1_300K: -15.26

I need (ignoring the first line) to sort the data according to the values in the second column from most negative to positive, and save it as a new log.

Would the sort utility work for such case?

sort -n ./file_unsorted > file_sorted
like image 812
James Starlight Avatar asked Aug 03 '26 11:08

James Starlight


2 Answers

Command:

grep -v '#' file_unsorted | sort -t ':' -k 2,2n > file_sorted

file_unsorted:

MD_cluster10_1_300K: -34.48
MD_cluster11_1_300K: -40.31
MD_cluster1_1_300K: -40.23
MD_cluster2_1_300K: -37.86
MD_cluster3_1_300K: -25.65
MD_cluster5_1_300K: -11.91
MD_cluster6_1_300K: -0.50
MD_cluster8_1_300K: -16.62
MD_cluster9_1_300K: -15.26

Output (file_sorted):

MD_cluster11_1_300K: -40.31
MD_cluster1_1_300K: -40.23
MD_cluster2_1_300K: -37.86
MD_cluster10_1_300K: -34.48
MD_cluster3_1_300K: -25.65
MD_cluster8_1_300K: -16.62
MD_cluster9_1_300K: -15.26
MD_cluster5_1_300K: -11.91
MD_cluster6_1_300K: -0.50
like image 138
OSezer Avatar answered Aug 06 '26 08:08

OSezer


check this out too :

with sed

sed '1d' log.txt | sort -k2 -n

with tail

tail -n +2 log.txt | sort -k2 -n 

with awk

awk 'NR>1{print | "sort -k2 -n"}' log.txt 

output

MD_cluster11_1_300K: -40.31
MD_cluster1_1_300K: -40.23
MD_cluster2_1_300K: -37.86
MD_cluster10_1_300K: -34.48
MD_cluster3_1_300K: -25.65
MD_cluster8_1_300K: -16.62
MD_cluster9_1_300K: -15.26
MD_cluster5_1_300K: -11.91
MD_cluster6_1_300K: -0.50
like image 38
Freeman Avatar answered Aug 06 '26 08:08

Freeman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!