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

I am not sure if anyone have the similar issue,
$a = "/path/to/a/file"; $b = "/path/to/b/file"; $difference = system("$diff $a $b"); $difference2 = system("$diff -y $a $b");
The $difference printed in html file only shows the difference - top & bottom (maybe 2 or 3 lines from 1000 lines).

But the $difference2 printed in html file shows all the difference in 2 columns -left & right (1000 lines from 1000 lines).

The output is actually fine, since I dont want to compare line by line, as sometime one has some extra lines while the other dont,

So i want those extra lines which is in a and in b at the same file same time.

Here, I am wondering if it is possible to have the sort of combination of both $diff and $diff -y, having 2 left/right columns of difference in some lines only (not showing every lines)
if ($difference eq ''){ $difference = "Exactly Same.\n"; } push @table1, "<table id =\"Table1\">"; push @table1, "<tr><th>path A</th><th>A</th><th>path B</th><th>B</th>< +/tr>"; push @table1, "<tr><td colspan =\"4\">$difference</td></tr>";
So that, $difference will shown in 2 columns with the line of differences in file.

Any idea guys ?

Replies are listed 'Best First'.
Re: diff vs diff -y
by roboticus (Chancellor) on Oct 19, 2018 at 17:53 UTC

    MissPerl:

    While awaiting responses on my question, I whipped this up. You could use it as a base to work from:

    $ cat ~/bin/diffident_html.pl #!env perl # # diffident_html.pl <FName> <FName> # # Build a dumb HTML table showing the diff (via Algorithm::Diff) of tw +o files # use strict; use warnings; use Algorithm::Diff; my $LFName = shift // die "Expected two file names!"; my @lfile = readfile($LFName); my $RFName = shift // die "Expected *TWO* file names!"; my @rfile = readfile($RFName); my $diff = Algorithm::Diff->new( \@lfile, \@rfile ); $diff->Base(1); print qq{<html><body><table border="1">\n}; print qq{<tr><th colspan="2" align="left" bgcolor="#c0c0c0">$LFName</t +h>} . qq{<th colspan="2" align="left" bgcolor="#c0c0c0">$RFName</t +h></tr>\n}; print qq{<tr><th width="1%" align="right" bgcolor="#c0c0c0">#</th>} . qq{<th width="49%" align="left" bgcolor="#c0c0c0">Source</th +>} . qq{<th width="1%" align="right" bgcolor="#c0c0c0">#</th>} . qq{<th width="49%" align="left" bgcolor="#c0c0c0">Source</th +></tr>\n}; while ($diff->Next()) { if ($diff->Same()) { # Left and Right file have a block of identical lines print qq{<tr><td colspan="4" align="center">.&nbsp;.&nbsp;.&nb +sp;. } . qq{ block of matching lines .&nbsp;.&nbsp;.&nbsp;.</td>< +/tr>\n}; next; } my ($min1, $min2) = $diff->Get(qw( Min1 Min2 )); my @L = $diff->Items(1); my @R = $diff->Items(2); while (@L or @R) { print qq{<tr>}; if (@L) { print qq{<td align="right">$min1</td><td align="left">$L[0 +]</td>}; ++$min1; shift @L; } else { print qq{<td colspan="2" bgcolor="#c0c0c0">}; } if (@R) { print qq{<td align="right">$min2</td><td align="left">$R[0 +]</td>}; ++$min2; shift @R; } else { print qq{<td colspan="2" bgcolor="#c0c0c0">}; } print qq{</tr>\n}; } } print qq{</table></body></html>\n}; sub readfile { my $fname = shift; open my $FH, '<', $fname or die "Can't open file $fname: $!\n"; return <$FH>; }

    I simply took an Anonymous Monk's suggestion to use Algorithm::Diff to get the differences between two files, and from that glued the data together into an HTML table. If you wanted to *really* use it, you'd probably want to use a template handling package, work through the error cases, etc.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thank you @roboticus, wow algorithm::diff definitely new to me, and it seems pretty effective!

      That's a lot for me to learn in this code ! It's definitely useful as a newbie like me !

      I love Perl more now !

      I would like to know why is readfile sub is used for ?

      Why can't just use declaration in below?
      my $LFName = shift // die "Expected two file names!"; my @lfile = readfile($LFName); my $RFName = shift // die "Expected *TWO* file names!"; my @rfile = readfile($RFName);
      Since it's receiving input this way, I would like to ask,

      what is it's for remote files in remote server ?

      Will it be any difference ? Still using the same to readfile() ?

        MisPerl:

        The readfile subroutine has two purposes: First it abstracts away the fetching of the data that we'll compare with Algorithm::Diff, and secondly to reduce (even if only a little) code duplication.

        By replacing the readfile() routine, you can source the data from wherever you like, without impacting the diff table generator. The way it's currently written, it'll just read whatever file you specify on the local machine. But you can rewrite readfile() to fetch from a remote server or whatever you may like. The only thing readfile() needs to return is the data you want to take the difference of.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: diff vs diff -y
by hippo (Archbishop) on Oct 19, 2018 at 15:47 UTC
    I am wondering if it is possible to have the sort of combination of both $diff and $diff -y, having 2 left/right columns of difference in some lines only (not showing every lines)

    Do you mean something like this?

      Thanks hippo ! Close enough but not really . Still thanks for the help ! :)
Re: diff vs diff -y
by johngg (Canon) on Oct 19, 2018 at 17:52 UTC

    I'm not sure from your description but are you wanting to only show lines that are different? If so

    diff -y --suppress-common-lines file1 file2

    could be what you need. I hope I haven't misunderstood your requirement.

    Cheers,

    JohnGG

      Hi johngg, yes this is part of what i am looking for ! Thanks for the help ! :)
Re: diff vs diff -y
by LanX (Saint) on Oct 19, 2018 at 15:23 UTC
    This doesn't sound like a Perl question, diff is a unix tool.

    Maybe you should provide some output and intended input to make it clearer for us?

    Kind of an SSCCE?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: diff vs diff -y
by Anonymous Monk on Oct 19, 2018 at 15:29 UTC
      Thanks for the advice anonymous monk ! that's very nice module to learn ! :)