[英]Unix: How to sort a dat file and keep original line numbers
I have a large data file containing over a thousand entries. 我有一个包含一千多个条目的大数据文件。 I would like to sort them but maintain the original line numbers. 我想对它们进行排序,但保留原始行号。 For instance, 例如,
1:100
2:120
3:10
4:59
Where the first number is the line number, not saved in the data file, separated by a colon from the real number. 其中第一个数字是未保存在数据文件中的行号,与实数之间用冒号分隔。 I would like to sort it and keep the line numbers bound to their original lines, with an output of: 我想对其进行排序,并使行号绑定到其原始行,并输出:
2:120
1:100
4:59
3:10
If possible, I would like to do this without creating another file, and numbering them by hand is not an option for the data size I'm using. 如果可能的话,我希望不创建另一个文件就这样做,并且手动编号不是我正在使用的数据大小的选项。
Given a file test.dat
: 给定一个文件test.dat
:
100
120
10
59
... the command: ... 命令:
$ cat -n test.dat | sort --key=2 -nr
2 120
1 100
4 59
3 10
... gives the output that you seem to be looking for (though with the fields delimited by tabs, which is easily changed if necessary): ...提供您似乎正在寻找的输出(尽管字段由制表符分隔,如有必要,可以轻松更改):
$ cat -n test.dat | sort --key=2 -nr | sed -e's/\t/:/'
2:120
1:100
4:59
3:10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.