Compare two txt files line by line

When you need to compare two txt files, you might be about to try following ways,
1, Create a simple perl or python or php script by yourself.
2, Using google to find out some already existed scripts to do comparation for you.
3, Download some windows softwares to help compare the text files.

But frankly most of your work should be finished in a linux environment. So why not have a try to find out some already existed commands first. You simply can issue a command like this to find which command supplies the expected function:

[joseph@admon compare]# man -k compare | grep files
File::Compare        (3pm)  – Compare files or filehandles
bzcmp [bzdiff]       (1)  – compare bzip2 compressed files
bzdiff               (1)  – compare bzip2 compressed files
cmp                  (1)  – compare two files
cmp                  (1p)  – compare two files
comm                 (1)  – compare two sorted files line by line
diff                 (1p)  – compare two files

tiffcmp              (1)  – compare two TIFF files
zcmp [zdiff]         (1)  – compare compressed files
zdiff                (1)  – compare compressed files

Now your question is much clear. You can use either File::Compare to create a perl script or have a try with cmp, comm, diff to compare your two text files. Here I’ll give an example on “comm“.

[eden@admon files]# comm –help
Usage: comm [OPTION]… FILE1 FILE2
Compare sorted files FILE1 and FILE2 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.

-1              suppress lines unique to FILE1
-2              suppress lines unique to FILE2
-3              suppress lines that appear in both files
–help     display this help and exit
–version  output version information and exit

Report bugs to <bug-coreutils#gnu.org>.

Please refer to “man comm” and “info comm” for the full documentation. If the info and comm programs are properly installed on your operation system, please follow these links for sample usages:

$ comm -23 allby.peter.sort done.joseph.sort > compared.lines.list
This command will generate a file named “compared.lines.list“, it contains the lines that exist in allby.peter.sort but nonexist in done.joseph.sort.

Pay attention to the suffix “sort”: Before using comm, you need to sort the file first in order.
$ sort allbypeter > allbypeter.sort

Share Button

5 thoughts on “Compare two txt files line by line

  1. Hi
    I have a formatted file and i want to see the difference by row1 of file 1 with row1 of file2. Then column1 of row 1 with column1 of row2 and soon.

    Matching field should be marked as P and unmatching field should be marked as F

    File 1
    1,2,3,5,8

    File 2
    1,2,4,6,,

    Output
    P,P,F,F,F,

    Please help

  2. Since it’s a more specific senario, I’d suggest you create a script to do that. Good luck! -joseph

  3. Great remarkable things here. I’m very glad to look your article. Thank you so much and i’m taking a look ahead to contact you. Will you kindly drop me a mail?

  4. It’s really a nice and useful piece of info. I’m happy that you simply shared
    this helpful info with us. Please keep us up
    too date like this. Thanks for sharing.

Leave a comment

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