World is Welcome To The World of Linux

Journey into the world of linux

Menu
  • About
  • Welcome
Menu

How To Find Common Lines Between Two Text Files In Unix

Posted on April 16, 2009 by Ganesh Sharma

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

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

1 thought on “How To Find Common Lines Between Two Text Files In Unix”

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

Leave a Reply Cancel reply

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

Recent Posts

  • Date Command Tutorial(Video)
  • grep Command Tutorial – 1(Video)
  • Introduction To ls Command(Video)
  • Chapter 3
  • Set Position Of Poll Module Into Joomla

Recent Comments

  • Kansas City Trailer Proz on Physical Volume In AIX – A Primer
  • Create volume group in AIX | myunixsheet on How To Create Volume Group
  • Restore of AIX backup on other Unix system - TecHub on What is mksysb And What Are Its Components
  • Firewall Unleashed - InfoSec Institute on Packet Filtering Firewall: An Introduction
  • Manwendra on Proxy Firewall and Gateway Firewall: Introduction

Archives

  • January 2019
  • June 2010
  • May 2010
  • February 2010
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008

Categories

  • 30 Days To Joomla WebSite Setup
  • A Journey To The World of Linux System Administration
  • Aix
  • Backups
  • Books
  • Firewalls
  • Introduction
  • Joomla
  • Joomla Backup
  • Joomla Web Technology
  • Linux
  • LPAR and Virtualization
  • LVM
  • Pluggable Authentication Modules
  • section navigator pro
  • Security
  • Security Knowledge Base
  • Tips and Tricks
  • Uncategorized
  • World is Welcome Products

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
©2023 World is Welcome To The World of Linux | WordPress Theme by SuperbThemes