http://www.perlmonks.org?node_id=918045

archer has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I have to compare the contents of two files and the output should be a report which should give the text that is present in file1 and not in file2 and vice versa. Which logic can i use to implement the same??


File 1: "This is a site for perl questions.Really helpful!!!"


File 2: "This is a site where you get your perl queries cleared.Really helpful!!!"


The output should be:
"for perl questions." - present in file 1 but not in file 2

"where you get your perl queries cleared." - present in file 2 but not in file 1
How to achieve this???

Replies are listed 'Best First'.
Re: Comparing the contents of two files
by moritz (Cardinal) on Aug 02, 2011 at 12:58 UTC
Re: Comparing the contents of two files
by derby (Abbot) on Aug 02, 2011 at 14:27 UTC

    Not a perl solution but I prefer to use sdiff for things like this.

    -derby
Re: Comparing the contents of two files
by JavaFan (Canon) on Aug 02, 2011 at 16:57 UTC
    There's a standard POSIX utility that does that: comm. It assumes your files are sorted (but if not, that's easy to do). In bash, you can write:
    comm <(sort file1) <(sort file2)
    and it prints three columns, one with lines only in file1, one with lines only in file2, and one with lines in both files.
Re: Comparing the contents of two files
by onelesd (Pilgrim) on Aug 02, 2011 at 21:04 UTC

    Your example shows differences on a single line, but your question asks about differences in a file. These will require different solutions, but maybe you are looking for a solution that handles both.

    Like another has suggested, I would look at POSIX's comm, and then perhaps run the results from that through String::Diff.