sorting - Linux sort command: keys with the same start but different length are not sorted in the same order -


i trying sort 2 files in order join them. of keys sorting similar , seems causing issues. example have 2 keys a1ke , a1k3-b3. using command:

sort -nk1 file.txt -o file.txt 

in 1 file appear in order , in other appear in reverse. causing issues when try join files.

how can sort these files in same order?

thanks

do not use "-n" option, compares according string numerical value.

-n compare according arithmetic value initial numeric string consisting of optional white space, optional - sign, , 0 or more digits, optionally followed decimal point , 0 or more digits. 

your keys strings, not numbers.

instead, should do:

sort -k1 file.txt -o file.txt 

additional info:

you can see sort considers keys identical when -n used doing unique sort:

sort -un file 

you see a1k3-b3 , a1ke considered equal (and therefore 1 emitted). if instead do:

sort -u file 

the result contain both a1k3-b3 , a1ke, want.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -