Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix sort the key of the combination of alphanumeric character and ':', '/'

Tags:

unix

sorting

I am trying sort the text file using the UNIX sort command (GNU 5.97 or 7.4) according to ASCII code. The lines in the file have a single column, which is used as the key in sort.

chr1:110170896:NM_004037:0:1:0/1
chr1:110170897:NM_004037:0:1:0/1
chr11:10325325:chr11:0:1:0/1
chr11::0325325:chr11:0:1:0/1

The ascii code of : is 58, and 1 is 49. However, when I sort the file with sort -k 1,1 temp.txt, the output is like this,

chr11::0325325:chr11:0:1:0/1
chr1:110170896:NM_004037:0:1:0/1
chr1:110170897:NM_004037:0:1:0/1
chr11:10325325:chr11:0:1:0/1

From the result, I have no idea how sort determines the order between 1 and :. If there were any fixed order, the first and the forth lines should be placed together.

Ideally, I hope to sort the key from the left character to the right character according to the ASCII code.

like image 653
Xatan Avatar asked Nov 04 '10 02:11

Xatan


People also ask

How do I sort alphanumeric strings in Linux?

You can always perform sort with argument -V to sort alphanumeric string.. You should mention that according to the manual, '-V' stands for "--version-sort" and is descrived as "natural sort of (version) numbers within text" -- taken from Ubuntu 20.04 standard repository.

How to sort a particular column in Unix?

5. -k Option: Unix provides the feature of sorting a table on the basis of any column number by using -k option. Use the -k option to sort on a certain column. For example, use “-k 2” to sort on the second column.

Which command is used for sorting?

Sort-r command is used to sort the lines of data in a file in reverse order.


1 Answers

how about

 sort -t : -k 1 filename

using the : as a field delimiter

like image 186
jim mcnamara Avatar answered Oct 27 '22 02:10

jim mcnamara