How to sort file with shared tab in nth column using cygwin sorting?

I have a huge delimited file that I want to sort by its 2nd column. I need to use a tab character as a field separator in a cygwin sort. So I need something like this:

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

But the command line evaluates "\ t" literally, and not as a tab character. Please note that I need to do this on a computer running Windows Cygwin. Variations such as

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

does not work, and does not put it in a cmd file with the actual tab instead of \ t above.

Edit: a solution using a DOS shell or a Cygwin bash shell is fine.

+7
sorting cygwin
source share
5 answers

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

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

(The "real" tab is between quotation marks.)

To get the tab, I type CTRL-V, TAB (CTRL-V and TAB).

EDIT: Now I tested it on a Windows computer from the cygwin command line, and it works there (as I expected, bash is bash).

+13
source share

You need to add the $ sign in front of \ t to enable line interpolation, so the tab is actually sent for sorting. This should work in any terminal:

 sort -t $'\t' -k 2,2 in.txt > out.txt 
+10
source share

On the Windows command line, the simplest solution I found is to disable tab execution first:

 cmd /f:off 

Then you can enter an alphabetic tab character.

+1
source share

I need a solution to sort GnuWin32 on Windows, but none of the above solutions worked for me on the command line. But the next batch file (.bat) worked, which is what I wanted. Enter a tab character in double quotation marks.

C:> cat foo.bat

sort -k3 -t "" tabfile.txt

0
source share

Does anyone see the irony here?
You have to jump through the hoops to make the tab character be a tab ...

At the windows command prompt, I was able to do this using:
c: \ bin \ sort -t "(actual tab)", but only after running cmd / f: off (as above)

In the bat file for Windows, I was able to do the same while a text editor (notepad2 :-) was installed to insert tabs as tabs, not spaces.


There are some useful tips for using -t $ '\ t ", but I tried about 2 ^ 16 combinations of this without any luck or remaining hair.

0
source share

All Articles