tags:

views:

677

answers:

2

I have a huge tab separated file which I want to sort on its 2nd column. I need to use the tab character as the field delimiter in cygwin sort. So I need something like this:

sort -t \t -k 2,2 in.txt > out.txt

But the command prompt evaluates '\t' literally and not as the tab character. Note that I need to do this on a Windows machine running Cygwin. Variations such as

sort -t "\t"
sort -t \"\t\"

don't work, neither does putting this in a cmd file with an actual tab in place of the \t above.

Edit: A solution using either the DOS shell or the Cygwin bash shell is fine.

+4  A: 

On my machine (Mac bash prompt, GNU sort ...) this works:

sort -t '   ' -k 2,2 in.txt > out.txt

(A "real" tab between the quotes.)

To get the tab there I type CTRL-V, TAB (CTRL-V followed by TAB).

EDIT: I've now tested it on a Windows machine from the cygwin prompt and it works the same there (as I expected, bash is bash).

PEZ
+3  A: 

You need to add a $ sign in front of the \t to turn on string interpolation, so the tab actually gets sent to sort. This should work in any terminal:

sort -t $'\t' -k 2,2 in.txt > out.txt
Joakim Lundborg
This does not work on the Windows command prompt. The error I get is: "sort: multi-character tab `$\\t'"
Nikhil
I don't include the windows command prompt in "any terminal" =)
Joakim Lundborg