使用未知分隔符的Unix排序(最后一列) [英] Unix sort using unknown delimiter (last column)
问题描述
我的数据如下:
Adelaide Crows 5 2 3 0 450 455 460.67 8
Essendon 5 5 0 0 622 352 955.88 20
Fremantle 5 3 2 0 439 428 598.50 12
当您可以看出,空格和制表符混合在一起。我需要能够对最后一列降序进行排序。因此输出看起来像这样:
As you can tell, there is a mixture of spaces and tabs. I need to be able to sort the last column descending. So the ouput looks like this:
Essendon 5 5 0 0 622 352 955.88 20
Fremantle 5 3 2 0 439 428 598.50 12
Adelaide Crows 5 2 3 0 450 455 460.67 8
整个数据由
使用排序如何实现这一目标。我是否可以尝试使用$字符从行尾开始?在对最后一列进行排序之后,我还需要对第二列进行排序。因此,最后一列中的所有重复编号将在最后第二列中排序。到目前为止的代码:
Using sort how can I achieve this. Am i right in attempting to use the $ character to start from the end of line? I also need to sort the second last column after sorting the last column. Therefore any duplicate numbers in the last column will be sorted in the 2nd last column. Code so far:
sort -n -t$'\t' -k 9,9 -k 8,8 tmp
我如何考虑橄榄球队的名字将被视为空白?
How do I take into account that the football team names will count as whitespace?
这是正在排序的文件(文件名:'tmp')示例数据
Here is the file being sorted (filename: 'tmp') sample data
推荐答案
您可以使用 awk $复制最后一个字段到第一个位置c $ c>,然后按
按第一个字段排序
,使用 cut
去除第一个字段。 / p>
You could copy the last field into the first position using awk
first, then sort
by the first field, the get rid of the first field using cut
.
awk '{print($NF" "$0)}' sample.txt | sort -k1,1 -n -r -t' ' | cut -f2- -d' '
Port Adelaide 5 5 0 0 573 386 916.05 20
Essendon 5 5 0 0 622 352 955.88 20
Sydney Swans 5 4 1 0 533 428 681.68 16
Hawthorn 5 4 1 0 596 453 620.64 16
Richmond 5 3 2 0 499 445 579.68 12
..
..
这篇关于使用未知分隔符的Unix排序(最后一列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!