Linux Common Line Comparison

LinuxLinuxBeginner
Practice Now

Introduction

Welcome to the future tech arena – a place where the brightest minds compete to showcase their mastery of cutting-edge technology. In today's event, we delve into the world of Linux, where the command line is the battleground and the audience is eager to witness the display of prowess in file comparison.

You, a brilliant technologist, are about to demonstrate the power of the comm command in front of a live audience. Your objective is to manipulate and analyse text files swiftly and accurately using this versatile command. Prepare to engage in a thrilling performance that will earn you the admiration of tech enthusiasts and novices alike!


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/VersionControlandTextEditorsGroup -.-> linux/comm("`Common Line Comparison`") subgraph Lab Skills linux/comm -.-> lab-271251{{"`Linux Common Line Comparison`"}} end

Prepare Your Text Files

In this step, you will create two text files that you will use throughout this lab to demonstrate the comm command. You will generate these files with a list of common Linux commands and some unique entries in each file.

First, create your working directory and two text files using the following commands:

mkdir -p ~/project/comm-lab
cd ~/project/comm-lab
echo -e "ls\ncd\npwd\nmkdir\ntouch\ncomm\nsed\nawk" | sort > commands1.txt
echo -e "ls\ncd\npwd\ncomm\ngrep\nfind\nsed" | sort > commands2.txt

Now you have two files, commands1.txt and commands2.txt, with similar but not identical contents.

Introduction to the comm Command

Now that you have your files, it's time to introduce the comm command and its basic usage. The comm command is used to compare two sorted files line by line. By default, it outputs three columns: unique to file1, unique to file2, and common lines.

Run the following command to see comm in action:

cd ~/project/comm-lab
comm commands1.txt commands2.txt

This will give you an output displaying the differences and similarities between the two files.

Extracting Unique and Common Lines

In this step, we'll explore how to use comm with options to extract either the unique or common lines. The options -1, -2, and -3 will suppress the corresponding columns.

First, let's extract the lines that are unique to commands1.txt:

comm -23 commands1.txt commands2.txt > unique_to_file1.txt

Next, find the lines that are common to both files:

comm -12 commands1.txt commands2.txt > common_lines.txt

Summary

In this lab, we explored the use of the comm command in the Linux command line to compare and analyze the content of text files quickly and efficiently. You successfully demonstrated how to create files, run the comm command to identify similarities and differences, and extract unique and common lines using options.

Your engaging performance in the future tech arena has enlightened the audience and showcased the utility of Linux command-line tools. This knowledge serves as a cornerstone in the mastery of Linux and in developing advanced automation scripts.

Other Linux Tutorials you may like