sort in cygwin

A very simple example that I don't understand.

A file test.txt contains two records:

RECORD1XYZ|FIELD2
RECORD1|FIELD2

I use the following command to sort:

sort -t"|" test.txt

and the result is:

RECORD1XYZ|FIELD2
RECORD1|FIELD2

how come?

goph9998Asked:
Who is Participating?
 
amit_gConnect With a Mentor Commented:
If you don't specify the key to sort on, the whole line is compared.

sort -t"|" -k1,1

info sort

has a good description of it.
0
 
goph9998Author Commented:
if I change "|" to ":" it works
0
 
amit_gConnect With a Mentor Commented:
That is because it is sorting the while line. The field separator doesn't matter.

When you compare

RECORD1XYZ|FIELD2
RECORD1|FIELD2

the first characters that are different is X and | and between those X < |

but when you compare

RECORD1XYZ:FIELD2
RECORD1:FIELD2

the first character that are different are X and : and between those X > :

ASCII(':') - 58
ASCII('X') - 88
ASCII('|') - 124
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.