in reply to Re^4: Need to have perl script to compare two txt files and print difference along with under which segment the difference is
in thread Need to have perl script to compare two txt files and print difference along with under which segment the difference is
Try
poj#!/usr/bin/perl use strict; use warnings; my @file = ('tst1.txt','tst2.txt'); my %compare = (); # inputs for my $n (0..$#file){ parse_file($n); } # output diff for my $segment (sort keys %compare){ for my $row (sort keys %{$compare{$segment}}){ my $rec = $compare{$segment}{$row}; if (defined $rec->[0] && defined $rec->[1]){ # matched } else { printf "%s %s\n",$segment,$row; } } } sub parse_file { my ($n) = @_; my $filename = $file[$n]; my $segment; open IN,'<', $filename or die "Could not open $filename : $!"; while (<IN>){ s/\s+$//; # trim trailing whitespace if (s/^\s+//){ ++$compare{$segment}{$_}[$n]; } else { $segment = $_; } } close IN; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Need to have perl script to compare two txt files and print difference along with under which segment the difference is
by User_04271983 (Initiate) on Jan 24, 2019 at 14:00 UTC | |
|
Re^6: Need to have perl script to compare two txt files and print difference along with under which segment the difference is
by User_04271983 (Initiate) on Jan 24, 2019 at 14:32 UTC | |
by poj (Abbot) on Jan 24, 2019 at 15:33 UTC |