Question: sort command question
0
gravatar for mxlsherry1992
8 months ago by
mxlsherry199230 wrote:

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 • 165 views
ADD COMMENTlink modified 8 months ago by Mensur Dlakic6.6k • written 8 months ago by mxlsherry199230
1
gravatar for cpad0112
8 months ago by
cpad011214k
India
cpad011214k wrote:
$ 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 COMMENTlink modified 8 months ago • written 8 months ago by cpad011214k

Thank you! it worked

ADD REPLYlink written 8 months ago by mxlsherry199230

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 REPLYlink written 8 months ago by RamRS30k
0
gravatar for Mensur Dlakic
8 months ago by
Mensur Dlakic6.6k
USA
Mensur Dlakic6.6k wrote:

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 COMMENTlink written 8 months ago by Mensur Dlakic6.6k

Thank u!! it worked!!

ADD REPLYlink written 8 months ago by mxlsherry199230
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1939 users visited in the last hour