The Wayback Machine - https://web.archive.org/web/20240724105349/https://www.geeksforgeeks.org/comm-command-in-linux-with-examples/
Open In App

comm command in Linux with examples

Last Updated : 19 Feb, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

comm compare two sorted files line by line and write to standard output; the lines that are common and the lines that are unique.

Suppose you have two lists of people and you are asked to find out the names available in one and not in the other, or even those common to both. comm is the command that will help you to achieve this. It requires two sorted files which it compares line by line.
Before discussing anything further first let’s check out the syntax of comm command:
Syntax :

$comm [OPTION]... FILE1 FILE2
  • As using comm, we are trying to compare two files therefore the syntax of comm command needs two filenames as arguments.
  • With no OPTION used, comm produces three-column output where first column contains lines unique to FILE1 ,second column contains lines unique to FILE2 and third and last column contains lines common to both the files.
  • comm command only works right if you are comparing two files which are already sorted.
  • Example: Let us suppose there are two sorted files file1.txt and file2.txt and now we will use comm command to compare these two.

    // displaying contents of file1 //
    $cat file1.txt
    Apaar 
    Ayush Rajput
    Deepak
    Hemant
    
    // displaying contents of file2 //
    $cat file2.txt
    Apaar
    Hemant
    Lucky
    Pranjal Thakral
    

    Now, run comm command as:

    // using comm command for 
    comparing two files //
    $comm file1.txt file2.txt
                    Apaar
    Ayush Rajput
    Deepak
                    Hemant
            Lucky
            Pranjal Thakral

    The above output contains of three columns where first column is separated by zero tab and contains names only present in file1.txt ,second column contains names only present in file2.txt and separated by one tab and the third column contains names common to both the files and is separated by two tabs from the beginning of the line.
    This is the default pattern of the output produced by comm command when no option is used .

    Options for comm command:

    1. -1 :suppress first column(lines unique to first file).
    2. -2 :suppress second column(lines unique to second file).
    3. -3 :suppress third column(lines common to both files).
    4. – -check-order :check that the input is correctly sorted, even if all input lines are pairable.
    5. – -nocheck-order :do not check that the input is correctly sorted.
    6. – -output-delimiter=STR :separate columns with string STR
    7. – -help :display a help message, and exit.
    8. – -version :output version information, and exit.

    Note :
    The options 4 to 8 are rarely used but options 1 to 3 are very useful in terms of the desired output user wants.

    Using comm with options

    1. Using -1 ,-2 and -3 options : The use of these three options can be easily explained with the help of example :

    //suppress first column using -1//
    $comm -1 file1.txt file2.txt
             Apaar
             Hemant
     Lucky
     Pranjal Thakral
    
    //suppress second column using -2//
    $comm -2 file1.txt file2.txt
            Apaar
    Ayush Rajput
    Deepak
            Hemant
    
    //suppress third column using -3//
    $comm -3 file1.txt file2.txt             
    Ayush Rajput
    Deepak       
            Lucky
            Pranjal Thakral
    

    Note that you can also suppress multiple columns using these options together as:

    //...suppressing multiple columns...//
    
    $comm -12 file1.txt file2.txt
    Apaar
    Hemant
    
    /* using -12 together suppressed both first
    and second columns */
    

    2. Using – -check-order option : This option is used to check whether the input files are sorted or not and in case if either of the two files are wrongly ordered then comm command will fail with an error message.

    $comm - -check-order f1.txt f2.txt
    

    The above command produces the normal output if both f1.txt and f2.txt are sorted and it just gives an error message if either of the two files are not sorted.

    3. Using – -nocheck-order option : In case if you don’t want to check whether the input files are sorted or not, use this option. This can be explained with the help of an example.

    //displaying contents of unsorted f1.txt//
    
    $cat f1.txt
    Parnjal 
    Kartik 
    
    //displaying contents of sorted file f2.txt//
    
    $cat f2.txt
    Apaar
    Kartik
    
    //now use - -nocheck-order option with comm//
    
    $comm - -nocheck-order f1.txt f2.txt
    Pranjal
            Apaar
                    Kartik
    
    /*as this option forced comm not to check
     the sorted order that's why the output 
    comm produced is also 
    not in sorted order*/

    4 . – -output-delimiter=STR option : By default, the columns in the comm command output are separated by spaces as explained above. However, if you want, you can change that, and have a string of your choice as separator. This can be done using the –output-delimiter option. This option requires you to specify the string that you want to use as the separator.
    Syntax:

    $comm - -output-delimiter=STR FILE1 FILE2

    EXAMPLE:

    //...comm command with - -output-delimiter=STR option...//
    
    $comm - -output-delimiter=+file1.txt file2.txt
    ++Apaar
    Ayush Rajput
    Deepak
    ++Hemant
    +Lucky
    +Pranjal Thakral
    
    /*+ before content indicates content of 
    second column and ++ before content 
    indicates content of third column*/ 
    

    So,that’s all about comm command and its options.


    Previous Article
    Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
cmp Command in Linux with examples
cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out whether the two files are identical or not. When cmp is used for comparison between two files, it reports the location of the first mismatch to the screen if difference is found and if no difference is found i.e the files compared are identical.cmp dis
5 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
df Command in Linux with examples
There might come a situation while using Linux when you want to know the amount of space consumed by a particular file system on your LINUX system or how much space is available on a particular file system. LINUX being command friendly provides a command line utility for this i.e df command that displays the amount of disk space available on the fi
8 min read
help Command in Linux with examples
If you are new to LINUX operating system and having trouble dealing with the command-line utilities provided by LINUX then you really need to know first of all about the help command which as its name says help you to learn about any built-in command. help command as told before just displays information about shell built-in commands. Here's the sy
3 min read
fgrep command in Linux with examples
The fgrep filter is used to search for the fixed-character strings in a file. There can be multiple files also to be searched. This command is useful when you need to search for strings which contain lots of regular expression metacharacters, such as "^", "$", etc. Syntax: fgrep [options] [ -e pattern_list] [pattern] [file] Options with Description
3 min read
rcp Command in Linux with examples
There comes a time while using LINUX when there is a need to copy some information stored in a file to another computer. This can be done simply using rcp command line utility . Obviously there exists some other methods to complete the above mentioned task which are more secure (like scp or rsync) but this command lets you do this in the simple way
4 min read
three90RightbarBannerImg