What is the difference between the Linux comm command options?

The Linux comm Command Options

The comm command in Linux is a powerful tool used to compare two sorted files and display the lines that are unique to each file, as well as the lines that are common to both files. The comm command offers several options that allow you to customize the output and control the comparison process.

1. Basic Usage

The basic syntax of the comm command is as follows:

comm [options] file1 file2

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

2. comm Command Options

The comm command supports the following options:

  1. -1: Suppress the output of the lines that are unique to file1.
  2. -2: Suppress the output of the lines that are unique to file2.
  3. -3: Suppress the output of the lines that are common to both file1 and file2.

Here's a Mermaid diagram that illustrates the different outputs of the comm command with these options:

graph TD A[file1] --> C[Unique to file1] B[file2] --> D[Unique to file2] C --> E[Common to both files] D --> E subgraph Options 1["-1"] --> C 2["-2"] --> D 3["-3"] --> E end

3. Examples

Let's consider two files, file1.txt and file2.txt, with the following contents:

# file1.txt
apple
banana
cherry
date
# file2.txt
banana
cherry
orange
  1. Default Output:

    $ comm file1.txt file2.txt
          apple
    banana
    cherry
          date
          orange
  2. Suppress Unique to file1:

    $ comm -2 file1.txt file2.txt
    banana
    cherry
          orange
  3. Suppress Unique to file2:

    $ comm -1 file1.txt file2.txt
          apple
    banana
    cherry
          date
  4. Suppress Common Lines:

    $ comm -3 file1.txt file2.txt
          apple
          date
          orange

By using these options, you can customize the output of the comm command to suit your specific needs when comparing two sorted files.

0 Comments

no data
Be the first to share your comment!