1

I have multiple .csv files in a directory called mydirectory. I want to sort all these files using some bash/awk/sed command first based on LeftChr column and then RightChr column and get the result.

>Id LeftChr LeftPosition    LeftStrand  LeftLength  RightChr    RightPosition   RightStrand
1979    chr1    825881  -   252 chr2    5726723 -
5480    chr2    826313  +   444 chr2    5727501 +
5492    chr5    869527  +   698 chr2    870339  +
1980    chr2    1584550 -   263 chr1    1651034 -
5491    chr14   1685863 +   148 chr1    1686679 +
5490    chr1    1691382 +   190 chr1    1693020 +

result

>Id LeftChr LeftPosition    LeftStrand  LeftLength  RightChr   RightPosition   RightStrand
     5490   chr1    1691382 +   190 chr1    1693020 +
     1979   chr1    825881  -   252 chr2    5726723 -
     1980   chr2    1584550 -   263 chr1    1651034 -
     5480   chr2    826313  +   444 chr2    5727501 +
     5492   chr5    869527  +   698 chr2    870339  +
     5491   chr14   1685863 +   148 chr1    1686679 +
1

You basically just need 'sort -k'

for f in *.csv; do
   # output of first line
   head -1 $f
   # output of any but first line, then sort after 2. then 6. column
   tail -n +2 $f | sort -k 2,6
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.