Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I diff two files in Perl?

Tags:

diff

perl

I have two text files which need to have the same values.

$ diff A.txt B.txt
4a5
> I have this extra line.
$

Open files in Perl

open (ONE, "<A.txt");
open (TWO, "<B.txt");

How can I do such a diff from within Perl? Does Perl have a inbuilt diff or do I need to use the unix diff utility? I don't want to implement my own diff algorithm for this.

I do need the information as to where my files differ, but I do not need to use the unix diff utility necessarily. That was just an example.

like image 973
Lazer Avatar asked Aug 27 '10 06:08

Lazer


People also ask

How do I compare two files perl line by line?

We can compare two files to see if they are the same in Perl using the compare() method. It is a method of the subroutine File::Compare . It compares files line by line. If a difference is detected, it stops.

How do you use file comparison?

On the File menu, click Compare Files. In the Select First File dialog box, locate and then click a file name for the first file in the comparison, and then click Open. In the Select Second File dialog box, locate and then click a file name for the second file in the comparison, and then click Open.

How do you compare two files to see if they are the same?

Comparison Using cmp GNU cmp compares two files byte by byte and prints the location of the first difference. We can pass the -s flag to find out if the files have the same content. Since the contents of file1 and file2 are different, cmp exited with status 1.


3 Answers

You could try using Text::Diff

Alternatively, the UNIX utility could be an option.

like image 68
cubic1271 Avatar answered Oct 22 '22 14:10

cubic1271


If I only needed to know that they were the same (i.e. not discover how they are different), I'd just use Digest::MD5 to see if they come up with the same digest. There's a vanishingly small chance that two different files could have the same MD5 digest, so you might even try Digest::SHA1.

If you want to find out which lines are different, then you can use Algorithm::Diff, perhaps in conjunction with Tie::File. However, there is also a diff program that comes with Algorithm::Diff if you don't have a diff tool on your target platform. Although you can shell out to that, you might just want to copy what it does into a subroutine. Text::Diff is built on top of Algorithm::Diff, so it might already do want you want.

like image 5
brian d foy Avatar answered Oct 22 '22 14:10

brian d foy


No, Perl doesn't have an inbuilt "diff" facility. Either you use an external module, or use Perl's data structures(hashes, arrays etc) or you create filehandles for both files, and iterate the files using the filehandle (while loops), comparing them line by line. This method assumes your files are sorted. Another not so elegant way is to call "diff" from Perl, but I advise against that.

Lastly, if Perl is not a must, just use the Unix diff utility (write a shell script).

like image 2
ghostdog74 Avatar answered Oct 22 '22 14:10

ghostdog74