do Sort of a file with multiple columns

Asked

Viewed 873 times

2

Hello need to do a Sort of my file, I have 3 columns: the first would be the chromosome (1,2,3,4,5.28,W,Z), the second would be the position on this chromosome and the last would be values.

When I do the sort -n I have as results the chromosomes drawn in 10,11,12,13.... but I want the order: 1,2,3...28,W,Z. The second column would like the values from the smallest to the largest. The third column does not matter.

Example of my input

10      247     0.02                      
10      445     0.04                   
10      447     0.08         
11      81     0.04      
11      91     0.01   
1      102     0.03  
1      105     0.05 

What would be the best command?

1 answer

2

You can use the option -V (--version-sort) of command sort to obtain the desired output.

Of the manual:

-V, --version-sort
         natural sort of (version) numbers within text

Assuming your file has a name ficheiroInput and contains the following content

A       131     0.01
10      247     0.02                      
10      447     0.04                   
10      445     0.08         
11       81     0.04      
11       91     0.01   
1       105     0.03  
1       102     0.05 
W       202     0.06

The command

cat ficheiroInput | sort -k1,1 -V -k2,2  

will produce this output:

1       102     0.05 
1       105     0.03  
10      247     0.02                      
10      445     0.08         
10      447     0.04                   
11       81     0.04      
11       91     0.01   
A       131     0.01
W       202     0.06
  • Thank you so much Bruno! I hadn’t tried the -V option. It’s exactly what I needed!

  • You’re welcome. I’m glad it worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.