如何使用 Linux 命令 Sort 根据第 4 列数字顺序对文本文件进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9015533/
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
how to use Linux command Sort to sort the text file according to 4th column, numeric order?
提问by mahmood
I have a file like this(which is space delimited):
我有一个这样的文件(用空格分隔):
AX-18 Chr1_419085 1 41908545 T C -1 98 0.51
AX-19 Chr1_419087 1 41908740 T C 0 15 0.067
AX-20 Chr1_419087 1 41908741 T C 0 13 0.067
and I want to use sort
command to sort the file according to 4th column. I looked it up everywhere on internet and I find different solutions which nun works!! I even find similar question in stackoverflow which the answer didn't work for me! so these are the commands that I'm using and are not working!
我想使用sort
命令根据第 4 列对文件进行排序。我在互联网上到处查找,我发现修女工作的不同解决方案!!我什至在 stackoverflow 中发现了类似的问题,但答案对我不起作用!所以这些是我正在使用但不起作用的命令!
sort -n -k 4,1 out1.txt
sort -n -k 4 out1.txt
sort -n -k4 out1.txt
sort -nk4 out1.txt
sort +4 out1.txt
so after running all these commands I get this output( which is identical to my input):
所以在运行所有这些命令后,我得到了这个输出(与我的输入相同):
AX-18 Chr1_419085 1 41908545 T C -1 98 0.51
AX-19 Chr1_419087 1 41908740 T C 0 15 0.067
AX-20 Chr1_419087 1 41908741 T C 0 13 0.067
I want to get an output like this:
我想得到这样的输出:
AX-19 Chr1_419087 1 41908741 T C 0 15 0.067
AX-20 Chr1_419087 1 41908740 T C 0 13 0.067
AX-18 Chr1_419085 1 41908545 T C -1 98 0.51
采纳答案by jaypal singh
sort -nk4 file
-n for numerical sort
-k for providing key
or add -r option
for reverse sorting
或添加-r option
反向排序
sort -nrk4 file
回答by sehe
It should be
它应该是
sort -k 4n out1.txt
Just tested this with GNU sort (--debug enabled):
刚刚用 GNU sort (--debug enabled) 测试了这个:
$ tac input | /bin/sort --debug -k 4n
/bin/sort: using simple byte comparison
/bin/sort: key 1 is numeric and spans multiple fields
AX-18 Chr1_419085 1 41908545 T C -1 98 0.51
________
___________________________________________
AX-19 Chr1_419087 1 41908740 T C 0 15 0.067
________
___________________________________________
AX-20 Chr1_419087 1 41908741 T C 0 13 0.067
________
___________________________________________
回答by Joni
sort
does not sort the file in-place. It outputs a sorted copy instead.
sort
不会就地对文件进行排序。它输出一个排序的副本。
You need sort -n -k 4 out.txt > sorted-out.txt
.
你需要sort -n -k 4 out.txt > sorted-out.txt
.
Edit:To get the order you want you have to sort the file with the numbers read in reverse. This does it:
编辑:要获得您想要的顺序,您必须使用反向读取的数字对文件进行排序。这样做:
cut -d' ' -f4 out.txt | rev | paste - out.txt | sort -k1 -n | cut -f2- > sorted-out.txt
cut -d' ' -f4 out.txt | rev | paste - out.txt | sort -k1 -n | cut -f2- > sorted-out.txt
回答by Malgi
It is helpful to mention that if the words in each line of file is separated by delimiter except ‘space', then we can specify delimiter using “-t” option:
值得一提的是,如果文件的每一行中的单词除 'space' 之外都由分隔符分隔,那么我们可以使用“-t”选项指定分隔符:
sort -n -t',' -k4 file -o outfile
We can get sorted output in any specified output file (using “-o” option) instead of displaying output on standard output.
我们可以在任何指定的输出文件中获得排序输出(使用“-o”选项),而不是在标准输出上显示输出。