Issue
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
Solution
Awk solution:
$ awk '/^#/{next};{a[$2]=$1}END{PROCINFO["sorted_in"]="@ind_num_asc"; for (i in a)print a[i],i}' file
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
To see the above script formatted legibly, using GNU awk to pretty-print it:
$ awk -o- '/^#/{next};{a[$2]=$1}END{PROCINFO["sorted_in"]="@ind_num_asc"; for (i in a)print a[i],i}' file
/^#/ {
next
}
{
a[$2] = $1
}
END {
PROCINFO["sorted_in"] = "@ind_num_asc"
for (i in a) {
print a[i], i
}
}
Answered By - Ivan Answer Checked By - Katrina (WPSolving Volunteer)