2

I only want to sort a file by the second character in the second column by the number order. the sample file like this:

aa 19 aa 189 aa 167 ab 13 nd 23 at 32 ca 90 

I expect the result like

ca 90 at 32 ab 13 nd 23 aa 167 aa 189 aa 19 

I use the command sort -n -k 2.2,2.2 [filename]. But it shows me the result like this:

aa 167 aa 189 aa 19 ab 13 nd 23 at 32 ca 90 

It is not the right answer. Does anybody know what's wrong with my command?

2 Answers 2

3

The problem is that you didn't specify the correct column delimiter, and sort assumes it's a tab instead of a space.

sort -t ' ' -nk 2.2 

works just fine.

Edit: in my man page it says that any whitespace is counted as delimiter by default, but the fact is that adding -t ' ' solves it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for editing and answer. But the weird thing is if i only sort the second column using sort -n -k 2. It seems to know the delimiter is space. The result is no problem. Do you know the reason?
@stevezhou I confirm this behavior, but I'm not sure why it happens.
0
sort -t ' ' -k2.2,2.2 filename 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.