473,382 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

comparison of 2 text files

6
I have two text files. How do I compare using perl?

-Tilu
Jun 1 '07 #1
10 2405
miller
1,089 Expert 1GB
That depends. In what way do you want to compare them.

- Miller
Jun 1 '07 #2
Hi,

If you are running your script in a unix environment, then better use the diff command.

`diff file1 file2 > output.txt`
Jun 1 '07 #3
tilu
6
That depends. In what way do you want to compare them.

- Miller
I want to write a perl script to compare line by line the two text files.
Jun 1 '07 #4
You can use any pre-defined modules such as Text::Diff or you can modify/use the following coding

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $firstfile = 'test1.txt';
  5. my $secondfile = 'test2.txt';
  6.  
  7. open(F1, $firstfile) or die "Unable to open $firstfile $!\n";
  8. open(F2, $secondfile) or die "Unable to open $secondfile $!\n";
  9.  
  10. my @firstfile = <F1>;
  11. my @secondfile = <F2>;
  12.  
  13. if (@firstfile != @secondfile) {
  14.     print (@firstfile > @secondfile)
  15.         ? "File $firstfile has more lines than $secondfile\n"
  16.         : "File $secondfile has more lines than $firstfile\n";
  17. } else {
  18.     for my $i (0..$#firstfile) {
  19.         if ($firstfile[$i] ne $secondfile[$i]) {
  20.             print "Difference Found in Line-" . ($i+1) . ":\n$firstfile: $firstfile[$i]\n$secondfile: $secondfile[$i]\n";
  21.         }
  22.     }
  23. }
  24.  
  25. close (F1);
  26. close (F2);
  27.  
Jun 1 '07 #5
KevinADC
4,059 Expert 2GB
Hi,

If you are running your script in a unix environment, then better use the diff command.

`diff file1 file2 > output.txt`
Why is that suggestion "better" than using perl?
Jun 1 '07 #6
numberwhun
3,509 Expert Mod 2GB
Why is that suggestion "better" than using perl?
I would say its not "better" than using Perl, just much quicker as you wouldn't be re-inventing the wheel. Granted, I am one that is all for re-inventing the wheel in order to learn, but if you are needing a quick, right now fix, then I would certainly use the system diff utility.

jlk
Jun 1 '07 #7
tilu
6
Thanks.. Its useful.

--tilu


You can use any pre-defined modules such as Text::Diff or you can modify/use the following coding

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $firstfile = 'test1.txt';
  5. my $secondfile = 'test2.txt';
  6.  
  7. open(F1, $firstfile) or die "Unable to open $firstfile $!\n";
  8. open(F2, $secondfile) or die "Unable to open $secondfile $!\n";
  9.  
  10. my @firstfile = <F1>;
  11. my @secondfile = <F2>;
  12.  
  13. if(scalar(@firstfile) == scalar(@secondfile))
  14. {
  15.     for(my $i = 0; $i<=$#firstfile; $i++)
  16.     {
  17.         if($firstfile[$i] eq $secondfile[$i])
  18.         {
  19.         }
  20.         else
  21.         {
  22.             print "Difference Found in Line-".($i+1).":\n$firstfile: $firstfile[$i]\n$secondfile: $secondfile[$i]\n";
  23.         }
  24.     }
  25. }
  26. else
  27. {
  28.     (scalar(@firstfile)>scalar(@secondfile))?print "File $firstfile has more lines than $secondfile\n":print "File $secondfile has more lines than $firstfile\n";
  29. }
  30. close (F1);
  31. close (F2);
  32.  
  33.  
  34.  
Jun 2 '07 #8
KevinADC
4,059 Expert 2GB
I would say its not "better" than using Perl, just much quicker as you wouldn't be re-inventing the wheel. Granted, I am one that is all for re-inventing the wheel in order to learn, but if you are needing a quick, right now fix, then I would certainly use the system diff utility.

jlk
How is it quicker? That would only be true if a person already knows the shell commands and is using an OS that supports it. Nix users seem to think everyone uses nix.
Jun 2 '07 #9
miller
1,089 Expert 1GB
I want to write a perl script to compare line by line the two text files.
When I asked in what way you wanted to compare them, I suppose I should have been more specific. Generally when comparing files, you're either testing for equality or for difference.

For equality, there is a cpan module called File::Compare that I believe would do the job.

For difference, the Text::Diff module that Freedolen mentioned would almost certainly do the trick.

- Miller

Jun 3 '07 #10
AdrianH
1,251 Expert 1GB
How is it quicker? That would only be true if a person already knows the shell commands and is using an OS that supports it. Nix users seem to think everyone uses nix.
Nix users? ;) I've usually used a wildcard in front of that :D though it doesn't cover Linux quite right.

Sorry, but though these utilities came from an OS that is closer to the primordial ooze of computing OSs, there are plenty of gnu ports to most all if not all other OSs platforms.

See MinGW or Cygwin for a more packaged implementation, or look for individual binaries.

I've also found that once you understand the nix utils, you will find that most of your scripting needs are done for you, so it may be slightly longer short term, but long term you're set.


Adrian
Jun 3 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dave Brueck | last post by:
Below is some information I collected from a *small* project in which I wrote a Python version of a Java application. I share this info only as a data point (rather than trying to say this data...
0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere, SourceOffSite, VSS...
3
by: for.fun | last post by:
Hi everybody, I am looking for a XML comparison tool (I do not mean a standard char-by-char diff tool but a tool which understand XML syntax) More precisely, I can have serveral XML...
0
by: Tim Brooks | last post by:
All, I'm hoping one of you Xml or Data gurus can offer an opinion. I'm working on an app to basically compare two semi-structured data files (e.g. Excel / CSV) to one another. But I need to...
5
by: mayamorning123 | last post by:
A comparison among six VSS remote tools including SourceOffSite , SourceAnyWhere, VSS Connect, SourceXT, VSS Remoting, VSS.NET To view the full article, please visit...
0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere, SourceOffSite, VSS...
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
0
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com...
7
by: Alan | last post by:
Hi. I have programmed in C++ before, but I`m a couple of years out of practice. I am seeking some advice on getting started on a quickie project. . . . I have to read a 54MB text file and do a...
1
by: Lars B | last post by:
Hey guys, I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers. In my program I need to compare the size of a given file against...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.