Enhancing Skills

comm: Compare Two Sorted Files

Description:
The comm command compares two sorted files line by line and outputs three columns:

  1. Lines unique to the first file
  2. Lines unique to the second file
  3. Lines common to both files.

Command:

comm file1.txt file2.txt

Sample Input

file1.txt:

apple
banana
cherry

file2.txt:

banana
cherry
date

Sample Output:

        banana
        cherry
apple
        date

Explanation:

  • Column 1: Lines unique to file1.txt (apple).
  • Column 2: Lines unique to file2.txt (date).
  • Column 3: Lines common to both files (banana, cherry).

Options:

  • -1: Suppress the first column.
  • -2: Suppress the second column.
  • -3: Suppress the third column.

Example with Options

To display only lines that are common to both files:

comm -12 file1.txt file2.txt

Sample Output with -12:

banana
cherry

This command is useful for comparing files, finding unique or common lines between files, and for various text processing tasks.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.