Search code examples
linuxsortingmultiple-columns

Sort data by two columns in linux


I have a file as shown in the image below and I would like to sort this by 2 columns (chr) and then by 3rd column (bpos) in Linux. Please suggest to me what is the best way to get this.

enter image description here


Solution

  • sort can do it with:

    sort  -k2,2n -k3,3n file.txt
    
    • -k2,2n sort by 2nd column, numerically
    • -k3,3n then sort by 3rd column, numerically