Home  >  Article  >  Operation and Maintenance  >  Detailed explanation of linux sort multi-field sorting

Detailed explanation of linux sort multi-field sorting

小云云
小云云Original
2018-02-11 10:43:343619browse

The sort program that comes with most Linux distributions is very powerful. Here we only talk about multi-field sorting. sort has a parameter -k, which can specify fields. It has a relatively complicated syntax and is not within the text range. This article mainly introduces the analysis of Linux sort multi-field sorting examples and shares relevant code examples. I think it is quite good and has certain reference value. Friends who need it can refer to it. I hope it can help everyone.

The following is a piece of data (obtained from the gene, only as a demo), the file name is data

chr13 3008566 3008677
chr9 3024384 3024515
chr19 3157071 3157172
chr5 3236476
chr13 3041044 3041191
chr12 3045532
chr6 3087308 3087625
chr5 3109870 3110091
chr9 31154 54 3115531

Separate fields with spaces

Now I want to sort by chromosome first, and then sort the genes on the same chromosome by starting site. Then this is a multi-field sorting, and the second field is a number. Use the sort command as follows

sort -t ' ' -k1,1 -k2n,2 data

Where -t ' ' specifies the use of spaces to sort columns

  • -k1,1 Specify the first column to sort the data by keyword

  • -k2n,2 Specify the second column to sort the data by keyword

You can also use

sort +0 -1 +1n -2

The effect is the same.

Sort results

chr12 3045343 3045532
chr13 3008677
chr13 3041044 3041191
chr19 3157071 3157172
chr5 3109870 31 10091
chr5 3236386 3236476
chr6 3087308 3087625
chr9 3024384 3024515
chr9 3115454 3115531

Related recommendations:

vue-slicksort a vue.js drag Drag component

js Detailed explanation of the differences between various sorting methods and sort methods

Example detailed explanation of JavaScript array sorting reverse() and sort() methods

The above is the detailed content of Detailed explanation of linux sort multi-field sorting. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn