[英]Sort fields in a Linux file based on the highest to lowest
I have a file in which 3 fields are present. 我有一个文件,其中存在3个字段。
Field 1 has names, field 2 and 3 has numeric values. 字段1具有名称,字段2和3具有数值。 I need to sort all the fields based on the highest values present in it.
我需要根据其中的最高值对所有字段进行排序。
File1.txt File1.txt
ASANSOL 16.76 31.991
ASANSOL 16.72 33.687
ASANSOL 16.48 25.344
ASANSOL 16.74 30.777
BANGALORE 13.39 17.243
BANGALORE 14.34 20.852
BANGALORE 15.58 25.112
BANGALORE 14.59 34.752
BANGALORE 17.93 2.792
BHOPAL 16.53 13.487
BHOPAL 16.7 11.599
BHOPAL 16.65 16.003
BHOPAL 16.87 20.674
After sorting and taking unique values, the desired output should produce as below: 排序并取得唯一值后,所需的输出应如下所示:
ASANSOL 16.76 33.687
BANGALORE 17.93 34.752
BHOPAL 16.87 20.674
Help me how shall I go about this? 帮帮我,我该怎么做? #Script would be preferable
#Script更可取
Sorry I made an answer of not asked first. 抱歉,我没有先回答。 The right answer is simpler:
正确的答案比较简单:
awk '{ d1[$1] = $2; d2[$1] = $3 } END { for(x in d1) print x, d1[x], d2[x] }' <filename>
----------- Old answer ----------------- -----------旧答案-----------------
simple answer is: 简单的答案是:
cat <filename> | awk '{ if($2>$3) { max = $2 } else { max = $3 } print max, $0 }' | sort -r | sed 's/^[0-9.]* //'
如果您不想编写一个小的脚本来处理此问题,则可以使用awk进行排序: http : //www.gnu.org/software/gawk/manual/gawk.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.