19

I am trying to sort this file based on the fourth column. I want the file to reordered based on the values of the fourth column.

File:

2   1:103496792:A   0   103496792
3   1:103544434:A   0   103544434
4   1:103548497:A   0   103548497
1   1:10363487:T    0   10363487

I want it sorted like this:

1   1:10363487:T    0   10363487
2   1:103496792:A   0   103496792
3   1:103544434:A   0   103544434
4   1:103548497:A   0   103548497

I tried this command:

sort -t$'\t' -k1,1 -k2,2 -k3,3 -k 4,4 <filename>

But I get illegal variable name error. Can somebody help me with this?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Vignesh
  • 912
  • 6
  • 13
  • 24

2 Answers2

47

To sort on the fourth column use just the -k 4,4 selector.

sort -t $'\t' -k 4,4 <filename>

You might also want -V which sorts numbers more naturally. For example, yielding 1 2 10 rather than 1 10 2 (lexicographic order).

sort -t $'\t' -k 4,4 -V <filename>

If you're getting errors about the $'\t' then make sure your shell is bash. Perhaps you're missing #!/bin/bash at the top of your script?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
4

I believe you have an errant $ in your command.

Try:

sort -t\t -nk4
Phylogenesis
  • 7,775
  • 19
  • 27