Comm

From Vero - Wikipedia
Revision as of 15:42, 20 October 2025 by imported>John of Reading (Comparison to diff: Typo fixing, replaced: signficantly → significantly)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Template:Short description {{#invoke:other uses|otheruses}} Template:For Template:More footnotes Template:Lowercase title Template:Infobox software comm is a shell command for comparing two files for common and distinct lines. It reads the files as lines of text and outputs text as three columns. The first two columns contain lines unique to the first and second file, respectively. The last column contains lines common to both. Columns are typically separated with the tab character. If the input text contains lines beginning with the separator character, the output columns can become ambiguous.

For efficiency, standard implementations of <syntaxhighlight lang="text" class="" style="" inline="1">comm</syntaxhighlight> expect both input files to be sequenced in the same line collation order, sorted lexically. The sort command can be used for this purpose. The <syntaxhighlight lang="text" class="" style="" inline="1">comm</syntaxhighlight> algorithm makes use of the collating sequence of the current locale. If the lines in the files are not both collated in accordance with the current locale, the result is undefined.

The command is specified in the POSIX standard. It has been widely available on Unix-like operating systems since the mid to late 1980s. Originally implemented by Lee E. McMahon, the command first appeared in Version 4 Unix.<ref name="reader">Template:Cite tech report</ref> The version in GNU coreutils was written by Richard Stallman and David MacKenzie.<ref>Template:Cite web</ref>

Example

<syntaxhighlight lang="console"> $ cat foo apple banana eggplant $ cat bar apple banana banana zucchini $ comm foo bar

                 apple
                 banana
         banana

eggplant

         zucchini

</syntaxhighlight>

This shows that both files have one banana, but only bar has a second banana.

In more detail, the output file has the appearance that follows. Note that the column is interpreted by the number of leading tab characters. \t represents a tab character and \n represents a newline (Escape character#Programming and data formats).

0 1 2 3 4 5 6 7 8 9
0 \t \t a p p l e \n
1 \t \t b a n a n a \n
2 \t b a n a n a \n
3 e g g p l a n t \n
4 \t z u c c h i n i \n

Limits

Up to a full line must be buffered from each input file during line comparison, before the next output line is written.

Some implementations read lines with the function <syntaxhighlight lang="text" class="" style="" inline="1">readlinebuffer()</syntaxhighlight> which does not impose any line length limits if system memory suffices.

Other implementations read lines with the function fgets(). This function requires a fixed buffer. For these implementations, the buffer is often sized according to the POSIX macro <syntaxhighlight lang="text" class="" style="" inline="1">LINE_MAX</syntaxhighlight>.

Comparison to diff

Although also a file comparison command, diff reports significantly different information than <syntaxhighlight lang="text" class="" style="" inline="1">comm</syntaxhighlight>. In general, <syntaxhighlight lang="text" class="" style="" inline="1">diff</syntaxhighlight> is more powerful than <syntaxhighlight lang="text" class="" style="" inline="1">comm</syntaxhighlight>. The simpler <syntaxhighlight lang="text" class="" style="" inline="1">comm</syntaxhighlight> is best suited for use in scripts.

The primary distinction between <syntaxhighlight lang="text" class="" style="" inline="1">comm</syntaxhighlight> and <syntaxhighlight lang="text" class="" style="" inline="1">diff</syntaxhighlight> is that <syntaxhighlight lang="text" class="" style="" inline="1">comm</syntaxhighlight> discards information about the order of the lines prior to sorting.

A minor difference between <syntaxhighlight lang="text" class="" style="" inline="1">comm</syntaxhighlight> and <syntaxhighlight lang="text" class="" style="" inline="1">diff</syntaxhighlight> is that <syntaxhighlight lang="text" class="" style="" inline="1">comm</syntaxhighlight> will not try to indicate that a line has changed between the two files; lines are either shown in the "from file #1", "from file #2", or "in both" columns. This can be useful if one wishes two lines to be considered different even if they only have subtle differences.

Unlike for <syntaxhighlight lang="text" class="" style="" inline="1">diff</syntaxhighlight>, the exit code of <syntaxhighlight lang="text" class="" style="" inline="1">comm</syntaxhighlight> does not indicate whether the files match. As is typical, 0 indicates success, and other positive values indicate an error.

See also

References

Template:Reflist

Template:Wikibooks

Template:Unix commands Template:Plan 9 commands Template:Core Utilities commands