How To Find Common Lines Between Two Text Files In Unix

April 16th, 2009

Those working in Unix environment must be aware of the requirement to find common lines between two Unix files. In this article I’m gonna introduce you to solve this problem.

For our example, I have taken two files first and second. The contents of the files are as below:

wiw_labs:$ cat first
computer
modem
monitor
phone
switch

wiw_labs:$ cat second
cable
mobile
modem
phone
server

In Unix one command to deal with such issues is the “comm” command. This command finds out what is common between two files, what is unique to first file and what is unique to second file.
Here is the general syntax for this command:

wiw_labs:$ comm first_file second_file

The output is three column output. First column shows the lines unique to the first file only. The second column shows the lines unique to second file only. The third column shows the lines common to both files.

wiw_labs:$ comm first second
    cable
computer
    mobile
    modem
monitor
    phone
    server
switch

If you have a look on the output, you’ll see:
computer, monitor and switch are only in first file.
cable, mobile and server are only in second file.
modem and phone are in both files.

The first, second and third column are specified by number 1,2 and 3.
Simple formula is, whichever column you don’t want to see, pre-pend it with -.

So, if you want to see only the lines unique to first file, use -23(Means don’t show me lines unique to second file and common lines.).
If you want to see only the lines unique to second file, use -13(Means don’t show me lines unique to first file lines and common lines.).
If you want to see only the lines common to both files, use -12(Means don’t show me first file lines and common lines.).

How To See The Line Only in First File
For watching lines unique to first file only use -23.

wiw_labs:$ comm -23 first second
computer
monitor
switch

How To See The Line Only in Second File
For watching lines unique to first file only use -13.

wiw_labs:$ comm -13 first second
cable
mobile
server

How To See The Line Common To Both Files

wiw_labs:$ comm -12 first second
modem
phone

Tags: , , , , , ,
Posted in Tips and Tricks | 1 Comment »

Comments

One Response to “How To Find Common Lines Between Two Text Files In Unix”

  1. Understanding The diff Command In Unix | World is Welcome To The World of Unix!!! Says:

    […] will be able to understand the usage properly and benefit from it. The other usefule command is: comm command. Here you […]

Leave a Reply

 Comment Form 

 



More articles from the category: Tips and Tricks


What is The Difference Between exec and xargs

There are very small things in linux based systems which often consfuse the users. Here in this article I’m going to discuss the specific use of find command and difference between exec and xargs.

Understanding The diff Command In Unix

It’s not an uncommon requirement for Unix system administrators to know the difference between two files. The diff command in Unix serves the purpose. Here I am going to discuss the diff command. It’s quite common but little understood command. I hope after reading this article, the Unix visitors will be able to understand the usage properly and benefit from it. Here you go…

How To Find Common Lines Between Two Text Files In Unix

Those working in Unix environment must be aware of the requirement to find common
lines between two Unix files. In this article I’m gonna introduce you to solve this problem.

How To Join Two Files Vertically

In Unix, sometimes we come across situations where we need to join two files’ output side by side vertically. This is also referred to as vertical joining of files. In such situations, paste command comes handy. With the help of this command you not only can vertically join the files, but insert some delimiter as well….

How To Recover A Superblock

If fsck or mount commands give errors then pretty good chances are there that the superblock is corrupt. The dd command comes to rescue. We know that the superblock resides in 31st block also. So, to recover that we use the following command: # dd count=1 bs=4k skip=31 seek=1 if=/dev/my_lv  of=/dev/my_lv count: How many blocks […]

Ways To Zip The Directory Structure of Unix

Often its the requirement of system administrators to move the directories in between the servers. Also the confusion arises which utility to use for the best results. Normally zip, tar, cpio utilities are used the most. I’m writing a short introduction of zip and tar.

How To Split or Cut A File Vertically(Column wise)

In Unix environment it’s often required to print specific columns from a file. If the rows of file are having some proper delimiters then other Unix command like awk come into picture and quite handy. But if say you want to print out the 5th character and then 7th to 16th character, then other commands fail and in such situations the cut command comes to rescue. Here is short primer of cut command.