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.
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>
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.
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:
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With