2

This is a followup to this question : What does sort -n do with non numeric values?

Here is my input:

1-----***
R----****
1---*****

And I run:

sort -k 1.2,1.9n input.txt > output.txt

Here is my output:

1-----***
1---*****
R----****

And my expected output:

1---*****
R----****
1-----***

I expect this output since the ASCII value of * (42) is smaller than the value of - (45). However, it appears the R is being considered in the sort, but I thought 1.2,1.9 would exclude it. What am I doing wrong?

  • Did the answer you were directed to solve your first question? If so, please let me know and I will close it as a dupe. – terdon Oct 30 '14 at 20:06
  • Yes, it solved it in that I know there is an ascii comparison going on now, but I'm still not clear on the details. – ConditionRacer Oct 30 '14 at 20:06
4

You are using an invalid key for sort (invalid because it is not matching your input text). The info manual for sort suggests using the --debug option to investigate the behavior of what they key is matching. Using your command key, 1.2,1.9n, sort produces the following output:

% sort -k 1.2,1.9n --debug input.txt 
sort: using ‘en_US.UTF-8’ sorting rules
1-----***
 ^ no match for key
_________
1---*****
 ^ no match for key
_________
R----****
 ^ no match for key
_________

and appears to be falling back on a default sort using the whole field. If you omit the n from your key and just use 1.2,1.9 you instead get the following:

% sort -k 1.2,1.9 --debug input.txt 
sort: using ‘en_US.UTF-8’ sorting rules
1-----***
 ________
_________
R----****
 ________
_________
1---*****
 ________
_________

which is properly using the 2nd through 9th characters of the first field for the sort operation.

  • Nice, didn't know about --debug – ConditionRacer Oct 30 '14 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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