Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sort a tab separated file on the nth column using cygwin sort?

Tags:

sorting

cygwin

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.

like image 544
Nikhil Avatar asked Jan 09 '09 00:01

Nikhil


People also ask

How do I sort a column separated by a tab?

The first thing we need to do is to tell sort to use TAB as a column separator (column separated or delimited) which we can do using: sort -t $'t' <my-file> If our input file was comma separated we could have used: sort -t "," <my-file>

How do I sort a tab delimited file in Unix?

Sorting a tab delimited file using the Unix sort command is easy once you know which parameters to use. An advanced file sort can get difficult to define if it has multiple columns, uses tab characters as column separators, uses reverse sort order on some columns, and where you want the columns sorted in non-sequential order.

How do I sort a file in reverse order?

I want column 4 sorted before column 3, and column 4 to be sorted in reverse order: I want the file sorted this way: To sort the file that way we have to define the sort parameters like this: The first thing we need to do is to tell sort to use TAB as a column separator (column separated or delimited) which we can do using:

How do I sort my myfile file on the third column?

Will sort your myfile file on the third column if your file don't have any separator. [...] -k, --key=POS1 [,POS2] start a key at POS1 (origin 1), end it at POS2 (default end of line) [...] POS is F [.C] [OPTS], where F is the field number and C the character position in the field; both are origin 1.


1 Answers

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
like image 184
Joakim Lundborg Avatar answered Oct 12 '22 14:10

Joakim Lundborg