comm Command in Linux

Introduction to the Linux comm Command

This tutorial will guide you through using the Linux comm command, a vital tool for systemadmin tasks. You'll discover how to effectively compare and contrast two sorted text files. The comm command enables you to pinpoint unique and shared lines, streamlining text processing. We'll cover the command's syntax, purpose, and various options to customize the output. This practical guide will enhance your Linux text manipulation skills, making you more efficient at system administration.

Understanding the Purpose and Syntax of the comm Command

This section introduces the purpose and syntax of the comm command in Linux. As a systemadmin, mastering this command is crucial. The comm command facilitates line-by-line comparison of two sorted files.

The basic syntax is as follows:

comm [options] file1 file2

Where file1 and file2 represent the two sorted files you intend to compare.

The comm command produces a three-column output:

  1. Lines exclusively found in file1
  2. Lines exclusively found in file2
  3. Lines present in both file1 and file2

By default, all three columns are displayed. Options are available to tailor the output to your needs.

Example output:

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

In this instance, "apple", "cherry", and "fig" are unique to file1.txt, "banana" is unique to file2.txt, and "date" exists in both files.

Comparing and Contrasting Sorted Files with the comm Command

In this segment, we'll demonstrate how to use the comm command to compare and contrast two sorted files. This is a fundamental skill for any systemadmin working with Linux.

Firstly, let's create two sample text files, file1.txt and file2.txt, and populate them with 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 these files:

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

The output reveals:

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

You can further refine the output using these options:

  • comm -1 file1.txt file2.txt: Hides the column showing lines unique to file1.txt
  • comm -2 file1.txt file2.txt: Hides the column showing lines unique to file2.txt
  • comm -3 file1.txt file2.txt: Hides the column showing 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

Customizing the Output of the comm Command with Options for System Administrators

This section explores how to tailor the comm command's output using different options, a key skill for any systemadmin.

Let's start by creating 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 examine some of the available options with the comm command:

  1. Suppressing columns:

    • comm -1 file3.txt file4.txt: Suppresses the column showing lines unique to file3.txt
    • comm -2 file3.txt file4.txt: Suppresses the column showing lines unique to file4.txt
    • comm -3 file3.txt file4.txt: Suppresses the column showing lines common to both files
  2. Suppressing all column indicators:

    • comm -1 -2 -3 file3.txt file4.txt: Suppresses all column indicators
  3. Removing blank separators:

    • comm -w file3.txt file4.txt: Removes the blank space separators between columns
  4. Skipping order verification:

    • comm --nocheck-order file3.txt file4.txt: Skips order verification (assumes files are sorted), useful in some scripting scenarios.

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: Mastering the comm Command for Efficient System Administration

In this lab, you've gained a solid understanding of the comm command in Linux, a vital tool for any aspiring or experienced systemadmin. You've learned its purpose, syntax, and how to use it for comparing and contrasting two sorted files line by line. You now know how to identify unique and common lines between files. Moreover, you've mastered customizing the comm command's output using a variety of options, such as suppressing specific columns.

You've also put your knowledge into practice by comparing sample text files (file1.txt and file2.txt) and analyzing the different output formats achieved through different options. Understanding this command allows for easier automation through shell scripting and other systemadmin tasks. Remember that the comm command is most useful when dealing with sorted files; consider using the sort command beforehand if your files are not sorted. Knowing how to use the comm command will significantly improve your workflow as a systemadmin.

400+ Linux Commands