Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: Difference between join and comm

Tags:

bash

join

comm

# comm -12 /tmp/src /tmp/txt | wc -l
  10338
# join /tmp/src /tmp/txt | wc -l
  10355

Both the files are single columns of alphanumeric strings and sort-ed. Shouldn't they be the same?


Updated following @Kevin-s answer below:

cat /tmp/txt | sed 's/^[:space:]*//' > /tmp/stxt
cat /tmp/src | sed 's/^[:space:]*//' > /tmp/ssrc

and the result:

#join /tmp/ssrc /tmp/stxt | wc -l
516
# comm -12 /tmp/ssrc /tmp/stxt | wc -l
513

On manual inspection of the diff-s ... the results differ due to some whitespaces that were not taken out by the sed.

like image 492
Tathagata Avatar asked Jul 26 '26 16:07

Tathagata


1 Answers

There are a couple of differences between comm and join:

  1. comm compares whole lines; join compares fields within lines.
  2. comm prints whole lines; join can print selected parts of lines.

When you have a single column of data in each file, there is relatively little difference. When you have multiple columns, there can be a lot of difference.

Also note that under the right circumstances, join can output multiple copies of the data from one file while joining with different lines from the other file. This looks to me like your problem; you probably have some duplicate values in one of the files. Suppose you have:

src           txt
123           123
              123
              123

If you do comm -12 src txt, you will get one line of output; if you do join src txt, you will get three lines of output. This is expected.

The join command can also handle 'outer joins' where data is missing from the second file for a line in the first file (a LEFT OUTER JOIN in terms of SQL) or vice versa (a RIGHT OUTER JOIN), or both at once (a FULL OUTER JOIN).

All-in-all, join is a more complex command, but it is attempting to do a more complex job. Both are useful; but they are useful in different places.

like image 62
Jonathan Leffler Avatar answered Jul 28 '26 06:07

Jonathan Leffler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!