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.
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:
- Lines that are unique to
file1 - Lines that are unique to
file2 - Lines that are common to both
file1andfile2
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 tofile1.txtcomm -2 file1.txt file2.txt: Suppress the column of lines unique tofile2.txtcomm -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:
Suppress columns:
comm -1 file3.txt file4.txt: Suppress the column of lines unique tofile3.txtcomm -2 file3.txt file4.txt: Suppress the column of lines unique tofile4.txtcomm -3 file3.txt file4.txt: Suppress the column of lines common to both files
Suppress all column headers:
comm -1 -2 -3 file3.txt file4.txt: Suppress all column headers
Suppress blank separators:
comm -w file3.txt file4.txt: Suppress the blank space separators between columns
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.



