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@'?
LC_ALL=C sort
Somehow related to Sort behaves strangely on case sensitive sorting – manatwork Mar 07 '13 at 10:01od -c
should show. – vonbrand Mar 07 '13 at 10:17