A-  A  A+ RSS Feed

Deep Thoughts by Robert Felty

thoughts on wordpress, latex, cooking et alia
July 14th, 2008

sort using TAB as field separator in bash

I have run into this problem several times recently, and decided to finally write down the solution for myself rather than keep searching the internet for it.

This is the problem: if you want to sort a file that is tab-delimited (and some of the filelds contain spaces), then you must explicitly tell sort to use TABS as the field separator, otherwise it will use any whitespace character. For functions such as cut and paste, this can be done like so:

cut -f 1 -d '\t' file

where -f specifies the field number and -d specifies the field seperator.
The sort command uses the -t flag instead. So one would think that this would work:

#INCORRECT
sort -k 2 -t '\t' file

where -k specifies the field number and -t specifies the field separator
Unfortunately this does not work, because sort won’t accept ‘\t’, since it treats it as a multi-byte character. The solution is to place a $ before it, like so:

#CORRECT
sort -k 2 -t $'\t' file

The dollar sign tells bash to use ANSI-C quoting
From: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_03.html

3.3.5. ANSI-C quoting

Words in the form “$’STRING’” are treated in a special way. The word expands to a string, with backslash-escaped characters replaced as specified by the ANSI-C standard. Backslash escape sequences can be found in the Bash documentation.

So now I have the answer for myself the next time the problem arises. I hope someone else benefits as well.

4 Responses to “sort using TAB as field separator in bash”

  1. On September 9th, 2008 at 7:45 am
    Bookmarks about Tab wrote:

    [...] sort using TAB as field separator in bash http://blog.robfelty.com/2008/07/14/sort-using-tab-as-field-separator-in-bash/ – bookmarked by 6 members originally found by Undrdogofcorona on 2008-08-16 Alt-Tab for Mac http://feeds.ihelpstupidpeople.com/~r/IHelpStupidPeople/~3/338571621/alt-tab-for-mac.html – bookmarked by 4 members originally found by AllanahK on 2008-07-31 Tab Cleanup – July 18, 2008 http://ministryofpi.com/2008/07/18/tab-cleanup-july-18-2008/ – bookmarked by 2 members originally found by chrisa on 2008-07-22 New tab on Blat homepage http://blat.blat.co.za/2008/07/07/new-tab-on-blat-homepage/ – bookmarked by 1 members originally found by hanginoutwithsailors on 2008-07-10 [...]

  2. On September 15th, 2008 at 3:33 am
    User links about "separator" on iLinkShare wrote:

    [...] | user-saved public links | iLinkShare 4 votessort using TAB as field separator in bash>> saved by kenmcdev 1 days ago4 votesNew Wet Dust Separator from Flextraction>> saved by [...]

  3. On January 19th, 2009 at 5:46 am
    Recent URLs tagged Separator - Urlrecorder wrote:

    [...] recorded first by zivilist on 2009-01-15→ sort using TAB as field separator in bash [...]

  4. On October 5th, 2009 at 12:24 pm
    user wrote:

    thanks.it works

Leave a Reply

You can use some basic tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
To get pretty code, use <code lang='php' > (or 'css' or 'perl' or whatever)