Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am having difficulty with the Unix sort command when tab delimited fields are introduced.

If a file test.txt contains:

Hello (2)
Hello (1)
Hello

applying a sort:

sort test.txt

produces the output:

Hello
Hello (1)
Hello (2)

However, if a file test2.txt contains tab separated fields:

Hello (2)\tFred
Hello (1)\tFred
Hello\tFred

where \t is a tab character, applying a sort:

sort -t $'\t' -k 1,2 test2.txt

results in a different sort order.

Hello (1)   Fred
Hello (2)   Fred
Hello   Fred

Is there a way to use the sort command so that the tab delimited fields will be sorted like this:

Hello   Fred
Hello (1)   Fred
Hello (2)   Fred
share|improve this question
up vote 2 down vote accepted

sort uses your current locale for sorting. I am using LANG=en_GB.UTF-8 myself and I get your "undesired" result. Using the C locale, I get your desired result:

$ LANG=C sort -t $'\t' -k 1,2 test2.txt
Hello   Fred
Hello (1)       Fred
Hello (2)       Fred
share|improve this answer
    
A wonderful and easy solution. Thanks! – Chris C. Jul 28 '13 at 20:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .