Linux 根据列排序命令 bash 对制表符分隔的文件进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17430470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort a tab delimited file based on column sort command bash
提问by Vignesh
I am trying to sort this file based on the fourth column. I want the file to reordered based on the values of the fourth column.
我正在尝试根据第四列对这个文件进行排序。我希望根据第四列的值对文件进行重新排序。
File:
文件:
2 1:103496792:A 0 103496792
3 1:103544434:A 0 103544434
4 1:103548497:A 0 103548497
1 1:10363487:T 0 10363487
I want it sorted like this:
我希望它像这样排序:
1 1:10363487:T 0 10363487
2 1:103496792:A 0 103496792
3 1:103544434:A 0 103544434
4 1:103548497:A 0 103548497
I tried this command:
我试过这个命令:
sort -t$'\t' -k1,1 -k2,2 -k3,3 -k 4,4 <filename>
But I get illegal variable name error. Can somebody help me with this?
但我收到非法变量名错误。有人可以帮我解决这个问题吗?
采纳答案by John Kugelman
To sort on the fourth column use just the -k 4,4selector.
要对第四列进行排序,只需使用-k 4,4选择器。
sort -t $'\t' -k 4,4 <filename>
You might also want -Vwhich sorts numbers more naturally. For example, yielding 1 2 10rather than 1 10 2(lexicographic order).
您可能还想要-V哪个更自然地对数字进行排序。例如,产生1 2 10而不是1 10 2(字典顺序)。
sort -t $'\t' -k 4,4 -V <filename>
If you're getting errors about the $'\t'then make sure your shell is bash. Perhaps you're missing #!/bin/bashat the top of your script?
如果您收到有关 的错误,请$'\t'确保您的 shell 是 bash。也许您在#!/bin/bash脚本的顶部丢失了?
回答by Phylogenesis
I believe you have an errant $in your command.
我相信你$的命令有误。
Try:
尝试:
sort -t\t -nk4

