comm

select or reject lines common to two files

TLDR

Produce three tab-separated columns: lines only in first file, lines only in second file and common lines

>_ comm [file1] [file2]
copy

Print only lines common to both files

>_ comm -12 [file1] [file2]
copy

Print only lines common to both files, reading one file from stdin

>_ cat [file1] | comm -12 - [file2]
copy

Get lines only found in first file, saving the result to a third file

>_ comm -23 [file1] [file2] > [file1_only]
copy

Print lines only found in second file, when the files aren't sorted

>_ comm -13 <(sort [file1]) <(sort [file2])
copy

SYNOPSIS

comm [ ] file1file2

PROLOG

This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.

DESCRIPTION

The comm utility shall read file1 and file2, which should be ordered in the current collating sequence, and produce three text columns as output: lines only in file1, lines only in file2, and lines in both files.

If the lines in both files are not ordered according to the collating sequence of the current locale, the results are unspecified.

OPTIONS

The comm utility shall conform to the Base Definitions volume of POSIX.1 Section 12.2,Utility Syntax Guidelines.

The following options shall be supported:

" "10 Suppress the output column of lines unique to file1.

" "10 Suppress the output column of lines unique to file2.

" "10 Suppress the output column of lines duplicated in file1 and file2.

OPERANDS

The following operands shall be supported:

" file1 "10 A pathname of the first file to be compared. If file1 is ', the standard input shall be used.

" file2 "10 A pathname of the second file to be compared. If file2 is ', the standard input shall be used.

If both file1 and file2 refer to standard input or to the same FIFO special, block special, or character special file, the results are undefined.

STDIN

The standard input shall be used only if one of the file1 or file2 operands refers to standard input. See the INPUT FILES section.

INPUT FILES

The input files shall be text files.

ENVIRONMENT VARIABLES

The following environment variables shall affect the execution of comm:

" LANG "10 Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1 Section 8.2,Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.)

" LC_ALL "10 If set to a non-empty string value, override the values of all the other internationalization variables.

" LC_COLLATE "10
Determine the locale for the collating sequence comm expects to have been used when the input files were sorted.

" LC_CTYPE "10 Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments and input files).

" LC_MESSAGES "10
Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

" NLSPATH "10 Determine the location of message catalogs for the processing of LC_MESSAGES.

ASYNCHRONOUS EVENTS

Default.

STDOUT

The comm utility shall produce output depending on the options selected. If the , , and options are all selected, comm shall write nothing to standard output.

If the option is not selected, lines contained only in file1 shall be written using the format:



"%s \ n", < linein file1 >



If the option is not selected, lines contained only in file2 are written using the format:

"%s%s \ n", < lead >,< linein file2 >



where the string < lead >is as follows:

<tab> 10 The option is not selected.

"null string" 10 The option is selected.

If the option is not selected, lines contained in both files shall be written using the format:

"%s%s \ n", < lead >,< linein both >



where the string < lead >is as follows:

<tab><tab> 10 Neither the nor the option is selected.

<tab> 10 Exactly one of the and options is selected.

"null string" 10 Both the and options are selected.

If the input files were ordered according to the collating sequence of the current locale, the lines written shall be in the collating sequence of the original lines.

STDERR

The standard error shall be used only for diagnostic messages.

OUTPUT FILES

None.

EXTENDED DESCRIPTION

None.

EXIT STATUS

The following exit values shall be returned:

" 0" 6 All input files were successfully output as specified.

>0 6 An error occurred.

CONSEQUENCES OF ERRORS

Default.

Thefollowingsectionsareinformative.

APPLICATION USAGE

If the input files are not properly presorted, the output of comm might not be useful.

EXAMPLES

If a file named xcu contains a sorted list of the utilities in this volume of POSIX.1 a file named xpg3 contains a sorted list of the utilities specified in the X/Open Portability Guide, Issue 3, and a file named svid89 contains a sorted list of the utilities in the System V Interface Definition Third Edition:



comm xcu xpg3 | comm svid89



would print a list of utilities in this volume of POSIX.1 not specified by either of the other documents:

comm xcu xpg3 | comm svid89



would print a list of utilities specified by all three documents, and:

comm xpg3 svid89 | comm xcu



would print a list of utilities specified by both XPG3 and the SVID, but not specified in this volume of POSIX.1

RATIONALE

None.

FUTURE DIRECTIONS

None.

COPYRIGHT

Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, Copyright (C) 2013 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. (This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.unix.org/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .

SEE ALSO

cmp, diff, sort, uniq The Base Definitions volume of POSIX.1 Chapter 8,Environment Variables, Section 12.2,Utility Syntax Guidelines

Copied to clipboard
free 100$ digital ocean credit
Access all the data from your your phone. Works offline. No third party analytics.
Apple App Store Google Play Store
Simon Schubert - sschubert89@gmail.com