0

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
0

1 Answer 1

2

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
0

You must log in to answer this question.

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