Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • (edit I could not put each character to a different line in a comment) So why then 'a~' 'a' 'a2' 'aa' 'aa3' 'aa~' will be sorted as 'cat tmp2 | sort -k1,1' 'a' 'a~' 'a2' 'aa' 'aa~' 'aa3' ? Since the '~' is after any other character in the ascii table, 'aa~' should be sorted after 'aa3' for example? Commented Feb 7, 2015 at 21:00
  • @giulio. The ASCII table is only relevant for sorting in the C/POSIX locale. String comparison in other locales is a lot more complicated and tries and work the same as in natural languages. For instance spaces are ignored in a first instance in most locales which is why "b c" sorts after "bb". Commented Feb 7, 2015 at 21:05
  • Are you sure there is a difference between sort -k1,2 and sort -k1,1 -k2,2? At least from your description, I don't understand the difference between the two commands. diff <(sort -t $'\t' -k1,2 <<<"$content") <(sort -t $'\t' -k1,2 -k2,2 <<<"$content") and diff <(LC_ALL=C sort -t $'\t' -k1,2 <<<"$content") <(LC_ALL=C sort -t $'\t' -k1,2 -k2,2 <<<"$content") produced no output. Commented Sep 16, 2015 at 2:39
  • @Six, in the C locale, comparison is done byte-by-byte on the byte value, that is strcoll() (the comparison function used by sort) works the same as strcmp() there (and there only). So for sort -k1,1 -k2,2 and sort -k1,2 to produce different results, you need an input that contains characters that sort before the separator. In the case of TAB, that's going to be byte values 0 to 7 which are unlikely to be found in text, but you can compare on the output of printf 'a\1\tb\na\tc\n'. Commented Sep 16, 2015 at 8:53
  • @Six, see that other answer for more detailed explanations. Commented Sep 16, 2015 at 8:57