Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

up vote 1 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.