sort command question
2
1
Entering edit mode
4.9 years ago

Dear all, I had a "txt" format file, there are 6 columns in this file, and each column is separated by 'tab'. What I want to do is that I need to sort according to the third column (weight) and make it in descending order.

Here is the script I used: sort -t ' ' -k 3nr file.txt > output.txt

But it failed, because the file is separated by 'tab', not 'space', but sort -t could not recognize 'tab'...

So if you know there is any way I could modify this command...

Thank you!

fromNode    toNode  weight  direction   fromAltName toAltName
gene27800   gene27803   0.242239037515047   undirected  NA  NA
gene14069   gene24274   0.118796442936695   undirected  NA  NA
gene14069   gene27143   0.0263384423340508  undirected  NA  NA
gene14069   gene24130   0.0654074678531233  undirected  NA  NA
gene14069   gene3821    0.0380989989267045  undirected  NA  NA
gene14069   gene6806    0.0928347139289517  undirected  NA  NA
gene24274   gene17058   0.0359478700694831  undirected  NA  NA
gene24274   gene3821    0.0352256086861173  undirected  NA  NA
gene24274   gene6806    0.232437951139367   undirected  NA  NA
gene11428   gene17058   0.0423883022935711  undirected  NA  NA
gene10886   gene10013   0.119643192562815   undirected  NA  NA
gene23678   gene27143   0.150740856709077   undirected  NA  NA
gene23678   gene23683   0.24929646045785    undirected  NA  NA
gene17058   gene3821    0.0368572782031628  undirected  NA  NA
gene17058   gene6075    0.0260725092173397  undirected  NA  NA
gene27143   gene24130   0.0244502519723576  undirected  NA  NA
gene27143   gene23683   0.312512557714924   undirected  NA  NA
RNA-Seq • 1.1k views
ADD COMMENT
2
Entering edit mode
4.9 years ago
$ awk 'NR!=1 {print}' test.txt | sort -t $'\t' -k 3nr 
gene27143   gene23683   0.312512557714924   undirected  NA  NA
gene23678   gene23683   0.24929646045785    undirected  NA  NA
gene27800   gene27803   0.242239037515047   undirected  NA  NA
gene24274   gene6806    0.232437951139367   undirected  NA  NA
gene23678   gene27143   0.150740856709077   undirected  NA  NA
gene10886   gene10013   0.119643192562815   undirected  NA  NA
gene14069   gene24274   0.118796442936695   undirected  NA  NA
gene14069   gene6806    0.0928347139289517  undirected  NA  NA
gene14069   gene24130   0.0654074678531233  undirected  NA  NA
gene11428   gene17058   0.0423883022935711  undirected  NA  NA
gene14069   gene3821    0.0380989989267045  undirected  NA  NA
gene17058   gene3821    0.0368572782031628  undirected  NA  NA
gene24274   gene17058   0.0359478700694831  undirected  NA  NA
gene24274   gene3821    0.0352256086861173  undirected  NA  NA
gene14069   gene27143   0.0263384423340508  undirected  NA  NA
gene17058   gene6075    0.0260725092173397  undirected  NA  NA
gene27143   gene24130   0.0244502519723576  undirected  NA  NA

to keep header, use keep-header function from tsv-utils:

$ keep-header test.txt -- sort -t $'\t' -k 3nr
fromNode    toNode  weight  direction   fromAltName toAltName
gene27143   gene23683   0.312512557714924   undirected  NA  NA
gene23678   gene23683   0.24929646045785    undirected  NA  NA
gene27800   gene27803   0.242239037515047   undirected  NA  NA
gene24274   gene6806    0.232437951139367   undirected  NA  NA
gene23678   gene27143   0.150740856709077   undirected  NA  NA
gene10886   gene10013   0.119643192562815   undirected  NA  NA
gene14069   gene24274   0.118796442936695   undirected  NA  NA
gene14069   gene6806    0.0928347139289517  undirected  NA  NA
gene14069   gene24130   0.0654074678531233  undirected  NA  NA
gene11428   gene17058   0.0423883022935711  undirected  NA  NA
gene14069   gene3821    0.0380989989267045  undirected  NA  NA
gene17058   gene3821    0.0368572782031628  undirected  NA  NA
gene24274   gene17058   0.0359478700694831  undirected  NA  NA
gene24274   gene3821    0.0352256086861173  undirected  NA  NA
gene14069   gene27143   0.0263384423340508  undirected  NA  NA
gene17058   gene6075    0.0260725092173397  undirected  NA  NA
gene27143   gene24130   0.0244502519723576  undirected  NA  NA
ADD COMMENT
0
Entering edit mode

Thank you! it worked

ADD REPLY
0
Entering edit mode

If an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one answer if they all work.

Upvote|Bookmark|Accept

ADD REPLY
2
Entering edit mode
4.9 years ago
Mensur Dlakic ★ 28k

As long as there is a uniform tab or space separator between columns, you don't need to specify it as sort will assume that they are separating columns.

sort -k3nr file.txt > output.txt

But if you really want to do it, tab is specified as \t

sort -t $'\t' -k3nr file.txt > output.txt

The command above will work in bash but not (t)csh.

ADD COMMENT
0
Entering edit mode

Thank u!! it worked!!

ADD REPLY

Login before adding your answer.

Traffic: 2674 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6