comm command in Linux

  • Post author:
  • Post last modified:November 14, 2024
  • Reading time:2 mins read

1.0 comm

The comm command compares two sorted files and gives output in three columns. The first column has lines which are present only in the first file. The second column contains lines which are present only in the second file. The third column gives the lines which are common to both files. For example,

$ cat names
Alan Bloggs
Erika Mustermann
James Bond
Jane Doe
Jimmy Fernandes
Joe Bloggs
John Doe
John Roe
Max Mustermann
Richard Roe
Tommy Atkins
$
$ cat fames
Alice Newman
Erika Mustermann
Jane Doe
Jimmy Doe
Joe Bloggs
John Doe
John Roe
Max Mustermann
Richard Roe
Tommy Atkins
Tom Roe
$
$ comm names fames
Alan Bloggs
	Alice Newman
		Erika Mustermann
James Bond
		Jane Doe
	Jimmy Doe
Jimmy Fernandes
		Joe Bloggs
		John Doe
		John Roe
		Max Mustermann
		Richard Roe
		Tommy Atkins
	Tom Roe

It is possible to suppress printing of one or more columns by passing the column number as an argument.

$ # Suppress printing of the first column
$ comm -1 names fames
Alice Newman
	Erika Mustermann
	Jane Doe
Jimmy Doe
	Joe Bloggs
	John Doe
	John Roe
	Max Mustermann
	Richard Roe
	Tommy Atkins
Tom Roe
$ 
$ # Suppress printing of the first and second columns
$ comm -12 names fames
Erika Mustermann
Jane Doe
Joe Bloggs
John Doe
John Roe
Max Mustermann
Richard Roe
Tommy Atkins

Karunesh Johri

Software developer, working with C and Linux.
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments