1836874 Members
2152 Online
110110 Solutions
New Discussion

file comparing

 
koby aharon_2
Occasional Advisor

file comparing

Hi
I need a way to compare two files and see the differences side by side not useing "sdiff"
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor

Re: file comparing

It would help to know what you are trying to do that sdiff does not already do.
If it ain't broke, I can fix that.
koby aharon_2
Occasional Advisor

Re: file comparing

i need to compare two files find if there is a differences and build a table

group|New|Old

sdiff only compare lines side by side. if i add a line to one of the files that will move the lines compared to the other file i will get that the line do not match.
A. Clay Stephenson
Acclaimed Contributor

Re: file comparing

Actually, sdiff will already do this; it attempts to "resynchronize" to matching lines after listing inserted lines.

If it ain't broke, I can fix that.
Hein van den Heuvel
Honored Contributor

Re: file comparing

[this reply is also appended as text file to preserve spacing]

I think I can sympathize with the basic request, but a
little more information is needed to help define an answer.
Let me answer through a counter-example.

I while ago felt the need to compare two SAP 'R3.out' files
from rather different SAP systems. So there were going to be a bunch
of known differences. Too much for standard tools to be useful.

So my script started out by recognizing the key difference:
'SAP system name' and 'Instance name'. In the partial data sample
below those were 'XXX' and 'Dnn'. Data value with this strings
were all replaced by a generic {SID} and {INST} strings before comparing.


There were also missing lines to deal will, similar to the 'resync' that
sdiff does as mentioned in an earlier reply.
I did have one powerful bit of knowledge... the params were sorted by name.
So I could read left, read right in general but read more rights if
left became too high or read more lefts if the right seemed to escape

I also wanted the report as name/left/right
much similar to the group/old/new requested.
Here is some sample input (modfied to protect the innocent :-):

rslg/send_daemon/autostart 0
rslg/send_daemon/exe_file /usr/sap/XXX/SYS/exe/run/rslgsend
rslg/send_daemon/listen_port (!) 3700
rslg/send_daemon/pid_file /usr/sap/XXX/Dnn/data/rslgspid
rslg/send_daemon/status_file /usr/sap/XXX/Dnn/data/rslgssta
rslg/send_daemon/talk_port (!) 1300

Here is some sample output from my script:

Parameter XXX - Dnn YYY - D02
------------------------------ ? -------------------- ? --------------------
DIR_CLIENT_ORAHOME | /oracle/{SID} X /oracle/{SID}/901_64
DIR_ORAHOME | /oracle/{SID} X /oracle/{SID}/901_64
DIR_SERVER_ORAHOME | /oracle/{SID} X /oracle/{SID}/901_64
ES/SHM_MAX_PRIV_SEGS X 63 | 2
ES/SHM_PROC_SEG_COUNT X 64 | 3
ES/TABLE X SHM_SEGS | UNIX_STD
:
rslg/send_daemon/listen_port X 3700 X 3702
rslg/send_daemon/talk_port X 1300 X 1302

So the lines with 'port' got picked up, but the lines with 'pid_file' were
not because they were logically equal

And below is the script. Maybe it is enough to get you going.
Perhpas enough even to solve your problem (unlikely),
but hopefully at least enough to make you ask the right question!

Enjoy,
Hein.


#!/bin/perl
# }
$f1 = @ARGV[0];
$f2 = @ARGV[1];
$ALL = @ARGV[2];SAP System\s+(\w+)\s/);
die "Must provide two R3.out files to compare" unless $f2;
open (F1, $f1) || die "Error open file 1: $f1";
open (F2, $f2) || die "Error open file 2: $f2";

# Find system ID and first parameter
# (also pick up SAPLOCALHOST? )
while () {
$S1 = $1 if (/SAP System\s+(\w+)\s/);
$I1 = $1 if (/^INSTANCE_NAME\s+\(!\) (\w+)/);
last if (/^Param/);
}

while () {
$S2 = $1 if (/SAP System\s+(\w+)\s/);
$I2 = $1 if (/^INSTANCE_NAME\s+\(!\) (\w+)/);
last if (/^Param/);
}

$format = "%-30.30s %s %-20s %s %-20s\n";
print "\nColumn \"?\" legend: \"|\" = default, \"X\" = changed, \" \" = missing.\n\n";
printf $format, "Parameter", " ", "$S1 - $I1", " ", "$S2 - $I2";
printf $format, "------------------------------","?","--------------------",
"?","--------------------";
while () {
$v1 = " ";
if (/^(\S+).*( |\(!\)) (.{1,20})/) {
$k1 = $1;
$d1 = ($2 eq " ") ? "|" : "X";
$v1 = $3;
$v1 =~ s/$S1/{SID}/g;
$v1 =~ s/$I1/{INST}/g;
}
while ($k2 lt $k1) {
last unless ($_ = );
$v2 = " ";
if (/^(\S+).*( |\(!\)) (.{1,20})/) {
$k2 = $1;
$d2 = ($2 eq " ") ? "|" : "X";
$v2 = $3;
$v2 =~ s/$S2/{SID}/g;
$v2 =~ s/$I2/{INST}/g;
}
if ($k2 lt $k1) {
printf $format, $k2, " ", " ", $d2, $v2;
}
}
if ($k1 eq $k2) {
printf $format, $k1, $d1, $v1, $d2, $v2 if
# ($ALL eq "all" || ($v1 ne $v2 && (($d1.$d2 ne "||")||($ALL eq "more")));
# ($ALL || ($v1 ne $v2 && ($d1.$d2 ne "||") ) );
($ALL || ($v1 ne $v2) );
} else {
printf $format, $k1, $d1, $v1, " ", " ";
}
}
James A. Donovan
Honored Contributor

Re: file comparing

You might want to look at the 'comm' command.

$man comm

DESCRIPTION
comm reads file1 and file2, which should be ordered in increasing collating sequence (see sort(1) and Environment Variables below), and produces a three-column output:

Column 1: Lines that appear only in file1,
Column 2: Lines that appear only in file2,
Column 3: Lines that appear in both files.

If - is used for file1 or file2, the standard input is used.

Options 1, 2, or 3 suppress printing of the corresponding column. Thus comm -12 prints only the lines common to the two files; comm -23 prints only lines in the first file but not in the second; comm -123 does nothing useful.
Remember, wherever you go, there you are...