Bash

根據目錄中的兩列對多個 csv 文件進行排序

  • December 5, 2015

.csv在一個名為mydirectory. 我想先使用一些 bash/awk/sed 命令對所有這些文件進行排序,然後基於LeftChrcolumn 然後RightChrcolumn 並獲取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 +

結果

>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 +

你基本上只需要’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

引用自:https://unix.stackexchange.com/questions/247260