排序大文件(10G)

use*_*453 4 linux sorting large-files bigdata

我正在尝试对存储在文件中的大表进行排序.文件格式为(ID,intValue)

数据按照排序ID,但我需要的是intValue按降序对数据进行排序.

例如

ID  | IntValue
1   | 3
2   | 24
3   | 44
4   | 2
Run Code Online (Sandbox Code Playgroud)

到这张桌子

ID  | IntValue
3   | 44
2   | 24
1   | 3
4   | 2
Run Code Online (Sandbox Code Playgroud)

如何使用Linux sort命令执行操作?或者你推荐另一种方式?

Dum*_*001 15

如何使用Linux sort命令进行操作?或者你推荐另一种方式?

正如其他人已经指出的,看到man sort-k-t了解如何通过字符串中的一些特定元素进行排序命令行选项.

现在,sort它还具有帮助排序可能不适合RAM的大文件的功能.即-m命令行选项,它允许将已排序的文件合并为一个.(请参阅合并排序的概念.)整个过程非常简单:

  1. 将大文件拆分成小块.例如,使用split带有-l选项的工具.例如:

    split -l 1000000 huge-file small-chunk

  2. 对较小的文件进行排序 例如

    for X in small-chunk*; do sort -t'|' -k2 -nr < $X > sorted-$X; done

  3. 合并已排序的较小文件.例如

    sort -t'|' -k2 -nr -m sorted-small-chunk* > sorted-huge-file

  4. 清理: rm small-chunk* sorted-small-chunk*

您唯一需要特别注意的是列标题.


orb*_*boy 1

怎么样:

sort -t' ' -k2 -nr < test.txt
Run Code Online (Sandbox Code Playgroud)

其中测试.txt

$ cat test.txt 
1  3
2  24
3  44
4  2
Run Code Online (Sandbox Code Playgroud)

按降序排序(选项 -r)

$ sort -t' ' -k2 -nr < test.txt 
3  44
2  24
1  3
4  2
Run Code Online (Sandbox Code Playgroud)

虽然按升序排序(不带选项 -r)

$ sort -t' ' -k2 -n < test.txt 
4  2
1  3
2  24
3  44
Run Code Online (Sandbox Code Playgroud)

如果你有重复的

$ cat test.txt 
1  3
2  24
3  44
4  2
4  2
Run Code Online (Sandbox Code Playgroud)

像这样使用 uniq 命令

$ sort -t' ' -k2 -n < test.txt | uniq 
4  2
1  3
2  24
3  44
Run Code Online (Sandbox Code Playgroud)