How to sort and ignore spaces?

Asked by At

I'm trying to sort a file but I can't get the results I want. I have this file :

742550111       aaa aaa aaa aaa aaa     2008    3       1       1
5816470687      aa a dissertation for the       933     2       2       2

Each field is separated by a tabulation, and I would like to sort on the second column.

When I try sort test.txt -t\t -k 2, the output is the same as in the file. But the output I want to have is :

5816470687      aa a dissertation for the       933     2       2       2
742550111       aaa aaa aaa aaa aaa     2008    3       1       1

I think that's because sort ignores the spaces between the words.

So I tried with this command : LC_ALL=C sort test.txt -t\t -k 2, but it still doesn't work.

Do you have any ideas ?

1 Answers

2
Cyrus On Best Solutions

Bash replaces $'\t' with a real tab:

LC_ALL=C sort file -t $'\t' -k 2 

Output:

5816470687      aa a dissertation for the       933     2       2       2
742550111       aaa aaa aaa aaa aaa     2008    3       1       1