JuJu
JuJu

Reputation: 30

How to sort and ignore spaces?

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 ?

Upvotes: 0

Views: 1297

Answers (1)

Cyrus
Cyrus

Reputation: 88776

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

Upvotes: 3

Related Questions