Linux comm Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux comm command to compare and contrast two sorted text files. The comm command is a powerful tool that allows you to identify unique and common lines between two files. You will start by understanding the purpose and syntax of the comm command, and then explore how to customize its output using various options. This lab covers practical examples and use cases for the comm command, making it a valuable skill for text processing and editing tasks in a Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/head("`File Beginning Display`") linux/VersionControlandTextEditorsGroup -.-> linux/diff("`File Comparing`") linux/VersionControlandTextEditorsGroup -.-> linux/comm("`Common Line Comparison`") subgraph Lab Skills linux/cat -.-> lab-422613{{"`Linux comm Command with Practical Examples`"}} linux/head -.-> lab-422613{{"`Linux comm Command with Practical Examples`"}} linux/diff -.-> lab-422613{{"`Linux comm Command with Practical Examples`"}} linux/comm -.-> lab-422613{{"`Linux comm Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the comm Command

In this step, you will learn about the purpose and syntax of the comm command in Linux. The comm command is a powerful tool used to compare and contrast two sorted files, line by line.

The basic syntax of the comm command is:

comm [options] file1 file2

Here, file1 and file2 are the two sorted files you want to compare.

The comm command outputs three columns:

  1. Lines that are unique to file1
  2. Lines that are unique to file2
  3. Lines that are common to both file1 and file2

By default, all three columns are displayed. You can use various options to customize the output.

Example output:

$ comm file1.txt file2.txt
        apple
banana
        cherry
date
        fig

In this example, the lines "apple", "cherry", and "fig" are unique to file1.txt, the line "banana" is unique to file2.txt, and the line "date" is common to both files.

Compare and Contrast Two Sorted Files Using the comm Command

In this step, you will learn how to use the comm command to compare and contrast two sorted files.

First, let's create two sample text files, file1.txt and file2.txt, with some content:

$ cat > file1.txt
apple
banana
cherry
date
fig
$ cat > file2.txt
banana
cherry
date
grape

Now, let's use the comm command to compare the two files:

$ comm file1.txt file2.txt
        apple
banana
        cherry
date
        fig
        grape

The output shows:

  • Lines that are unique to file1.txt (prefixed with a single tab)
  • Lines that are unique to file2.txt (prefixed with a single tab)
  • Lines that are common to both files (no prefix)

You can also use various options to customize the output:

  • comm -1 file1.txt file2.txt: Suppress the column of lines unique to file1.txt
  • comm -2 file1.txt file2.txt: Suppress the column of lines unique to file2.txt
  • comm -3 file1.txt file2.txt: Suppress the column of lines common to both files

Example:

$ comm -1 -2 file1.txt file2.txt
date
$ comm -1 -3 file1.txt file2.txt
apple
fig
$ comm -2 -3 file1.txt file2.txt
banana
cherry
grape

Customize the Output of the comm Command with Options

In this step, you will learn how to customize the output of the comm command using various options.

First, let's create two more sample text files, file3.txt and file4.txt:

$ cat > file3.txt
apple
banana
cherry
date
fig
$ cat > file4.txt
banana
cherry
date
grape
kiwi

Now, let's explore some of the options available with the comm command:

  1. Suppress columns:

    • comm -1 file3.txt file4.txt: Suppress the column of lines unique to file3.txt
    • comm -2 file3.txt file4.txt: Suppress the column of lines unique to file4.txt
    • comm -3 file3.txt file4.txt: Suppress the column of lines common to both files
  2. Suppress all column headers:

    • comm -1 -2 -3 file3.txt file4.txt: Suppress all column headers
  3. Suppress blank separators:

    • comm -w file3.txt file4.txt: Suppress the blank space separators between columns
  4. Suppress line numbers:

    • comm --nocheck-order file3.txt file4.txt: Suppress line numbers (assumes the files are already sorted)

Example outputs:

$ comm -1 file3.txt file4.txt
        banana
        cherry
date
        grape
        kiwi
$ comm -1 -2 -3 file3.txt file4.txt
banana
cherry
date
grape
kiwi
$ comm -w file3.txt file4.txt
applebananacherrydate figbananacherrydate grape kiwi
$ comm --nocheck-order file3.txt file4.txt
apple
banana
cherry
date
fig
banana
cherry
date
grape
kiwi

Summary

In this lab, you learned about the purpose and syntax of the comm command in Linux, which is used to compare and contrast two sorted files, line by line. You explored how to use the comm command to identify lines that are unique to each file, as well as lines that are common to both files. Additionally, you learned how to customize the output of the comm command using various options, such as suppressing specific columns of output.

You also practiced using the comm command to compare and contrast two sample text files, file1.txt and file2.txt, and observed the different output formats based on the options used.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like