Test your knowledge!Take a quiz to access yourself.

Comm Command in Linux

The best thing about working on shell is, one can easily do tricky and complex tasks in one single line(most of the time). For example, if one wants to search for a string in all files in a given folder and replace them with the given input letter, string or paragraph, one line of shell commands is enough. Give me the honor to show you a good use of shell that might help making your hectic task easy some day:

Get the common lines out of two files in STDOUT or in a file

In linux, there is one simple command to get the lines are present in both files:

comm [Options] file1.txt file2.txt

I’ll not go digging deep to teach you every options that can be used with this command, instead i would like to stay stick to my motto, i.e, drag out common lines.

So, the option field i’ll use will be -12. So our new command is:

comm -12 file1.txt file2.txt

Let me explain how does this command work. Well, comm command whenever run against any two files, sorts the files and creates 3 column as its output. The first column(represented by numeric value 1) contains all the lines when both files are combined minus the lines unique to file 1. Similarly, The first column(represented by numeric value 2) contains all the lines when both files are combined minus the lines unique to file 2. There is a small difference in 3rd line(represented by value 3), it contains only those lines that are unique to each file(means no common lines at all).

Complicated??? Yeah, may look difficult to understand but the picture i created, will make it clear:

Regex

 

Given Circle 1 presents all the lines present in circle1.txt and same goes with circle2. Also, A represents lines unique to circle.txt only, B unique to circle2.txt and C got the common lines of both files.

comm -1 circle1.txt circle2.txt —  lines in C+ lines in B

comm -2 circle1.txt circle2.txt —  lines in A+ lines in C

comm -12 circle1.txt circle2.txt —  lines in C

comm -3 circle1.txt circle2.txt —  lines in A+ lines in B

Point to Note: This command runs the sort  command to both files before creating the 3 columns. So it might not help giving you every single common string present in both files.

Add a Comment

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