LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-09-2009, 03:02 PM   #1
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Rep: Reputation: 17
comparing two files in perl


Hi All,

I would like to ask opinion from perl experts.

I want to compare 2 files and show the differences in a text file.

For example, if i open File A an B in notepad

File A:
apple orange grape
apple orange grape kiwi
apple orange grape kiwi melon pineapple
orange grape kiwi pineapple
apple orange grape kiwi melon pineapple
apple orange grape kiwi


File B:
apple orange grape
apple orange grape kiwi
orange grape kiwi pineapple
apple orange grape kiwi melon pineapple
apple orange grape kiwi


Quote:
line3 is missing in File B
So if I did a File compare (line by line),
the differences will be in line3, line4, line5, line 6.


But I dont want it to be like that.

I want it to be like this

Quote:
line1: no differences
line2: no differences
line3: differences detected. Line 3 is missing from File B
line4: no differences
line5: no differences
line6: no differences

Can Text:: Diff able to perform the work.

Please advice.

John

Last edited by john83reuben; 10-09-2009 at 03:04 PM.
 
Old 10-09-2009, 05:37 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
You won't get exactly the output you want without some work manipulating the results from Text::Diff, but it will do a line by line match similar to diff

Code:
use Text::Diff;
print diff "t1.txt", "t2.txt", { STYLE => "Unified" };
will produce

Code:
--- t1.txt	Sat Oct 10 09:15:35 2009
+++ t2.txt	Sat Oct 10 09:15:44 2009
@@ -1,6 +1,5 @@
 apple orange grape 
 apple orange grape kiwi
-apple orange grape kiwi melon pineapple
 orange grape kiwi pineapple
 apple orange grape kiwi melon pineapple
 apple orange grape kiwi
There are also a few other styles of output.

You should be wary of using it on very large files.

Last edited by neonsignal; 10-09-2009 at 05:39 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about comparing one file to another in Perl HyperTrey Programming 4 11-20-2008 05:26 PM
Perl: comparing 2 files UnixKiwi Programming 14 07-05-2007 03:27 AM
Comparing text files... jong357 Slackware 14 03-31-2007 04:29 PM
perl files download instead of parsed on apache2 mod-perl2 not parsing perl files zeigerpuppy Debian 1 02-16-2006 05:31 AM
Comparing 2 Files xianzai Programming 2 05-23-2004 11:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration