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
  • 5,101

0 Answers0