Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comparing the contents of two files

by archer (Novice)
on Aug 02, 2011 at 12:55 UTC ( #918045=perlquestion: print w/ replies, xml ) Need Help??
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???

Comment on Comparing the contents of two files
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://918045]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-09-18 03:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (105 votes), past polls