Q. I'm trying to use diff command, but it is not working. I'd like to display those lines that are common to file1 and file2? How do I do it?
A. Use comm command; it compare two sorted files line by line. With no options, produce three column output. Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and column three contains lines common to both files.
To Display Those Lines That Are Common to File1 and File2
Type the command as follows:
$ comm /path/to/file1/ /path/to/file2
$ comm -1 /path/to/file1/ /path/to/file2
$ comm -2 /path/to/file1/ /path/to/file2
$ comm -3 /path/to/file1/ /path/to/file2
Where,
- -1 : suppress lines unique to FILE1
- -2 : suppress lines unique to FILE2
- -3 : suppress lines that appear in both files
You can also try out perl code (it was posted by someone at comp.unix.shell news group):
$ perl -ne 'print if ($seen{$_} .= @ARGV) =~ /10$/' file1 file2
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- My 10 UNIX Command Line Mistakes
- Linux: 20 Iptables Examples For New SysAdmins

- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
Facebook it - Tweet it - Print it -


{ 10 comments… read them below or add one }
And if you want to find the lines NOT common in 2 files, use diff
I used the line of perl to find the common lines between two files, but I needed it to ignore case. The solution, provided by mu was:
perl -ne ‘print if ($seen{lc $_} .= @ARGV) =~ /10$/’ file1 file2
Thanks mu!
Aengus
I am not able to find common lines between 2 files with comm -3.. I use diff instead:
Use regex of contents of the file instead of \d+
diff -y <(sort file1) out
And for finding lines that are present in file1 but not in file2:
diff –suppress-common-lines <(sort file1) ” > output
I don’t see where you specify file2 in either option. I’m interested in the second option.
Why not:
comm -1 -2 /path/to/file1/ /path/to/file2
This works in the command line on OSX for showing on the common lines to the 2 files. (though perhaps it is limited to OSX’s flavor of BSD — haven’t tested elsewhere)
It works only for sorted files, so you need:
/path/to/file1 | sort > /path/to/file1_sorted
/path/to/file2 | sort > /path/to/file2_sorted
comm -1 -2 /path/to/file1_sorted/ /path/to/file2_sorted
Ah, yes, good point. I was coming at it from two files of stats for which I had removed duplicate lines and sorted.
Thanks for the Perl code. :-)
It works.
This script takes two files as arguments $1 and $2 and prints common lines in the
two files.
===============================
while read line
do
echo “Searching for : ”
echo $line
while read lines
do
if [ "$lines" = "$line" ]
then
echo $line
fi
done < $2
done < $1
======================================
save this into a file then run it ./anyname file1 file2
NOTE : The input files must be intended i.e. no extra space in lines (i.e before or after lines.)
i tried it but it is not working