2

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

5

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 to the next line in a_file
  2. ff < gg : print ff, move to the next line in a_file
  3. hh > gg : print gg, move to the next line in b_file
  4. hh < tt : print hh, move to the next line in a_file
  5. bb < tt : print bb, move to the next line in a_file
  6. No a_file left, so print the rest of b_file.
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.