1

As sort's man page says:

-m, --merge
    merge already sorted files; do not sort

Here are my two simple text files and the result of sort command with -m option:

soroush@pop-os:~/Desktop$ cat a_file.txt 
aa
ff
hh
bb
soroush@pop-os:~/Desktop$ cat b_file.txt 
gg
tt
ss
ii
cc
soroush@pop-os:~/Desktop$ sort -m a_file.txt b_file.txt 
aa
ff
gg
hh
bb
tt
ss
ii
cc

I expected to see this output:

aa
ff
hh
bb
gg
tt
ss
ii
cc

Could anyone explain this behavior please?

1 Answer 1

4

Merging assumes the files are already sorted: "merge already sorted files; do not sort", so will attempt to merge them into alphabetic order. It is not a simple concatination. So in your example:

  1. aa < gg : print aa move on a_file
  2. ff < gg : print ff move on a_file
  3. hh > gg : print gg move on b_file
  4. hh < tt : print hh move on a_file
  5. bb < tt : print bb move on a_file
  6. No a_file left, so print the rest of b_file.
1
  • Thanks. "do not sort" at the end made me wrong... I should have thought more wisely. Otherwise why this option even exists !
    – S.B
    Mar 21 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.