2

I'm trying to sort a text file using certain columns in the file as a sort key. Since the file doesn't have separators and fields sometimes stick to each other, I have to convert spaces to some distinctive character (like '@') and then extract columns from the whole string as from the field #1. I've stumbled upon a problem that sort orders strings in some manner unknown to me. Example:

$ cat aaa.txt 
@1
@2
 1
 2
1 
2 

Now replace spaces by '@':

$ sed y/\ /@/ aaa.txt
@1
@2
@1
@2
1@
2@

Now try to sort them:

$sed y/\ /@/ aaa.txt | sort
@1
@1
1@
@2
@2
2@

And the result is embarassing. Lexically, '@' must be before or after the '1'. If '@'>'1' then why strings '@1' are before '1@'? If '@'<'1' then why strings '@2' are after '1@'?

mbaitoff
  • 4,911
  • 8
  • 28
  • 32
  • 3
    `LC_ALL=C sort` Somehow related to [Sort behaves strangely on case sensitive sorting](http://unix.stackexchange.com/questions/19856/sort-behaves-strangely-on-case-sensitive-sorting) – manatwork Mar 07 '13 at 10:01
  • Any funny characters in the file? `od -c` should show. – vonbrand Mar 07 '13 at 10:17
  • See http://unix.stackexchange.com/a/43466/6622 (actually your question can be considered a dup) – poige Mar 07 '13 at 12:55

0 Answers0