| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<[email protected]> |
Given below is another way to accomplish the task at hand, which is based on the output of the 'diff' command and using a state-machine approach to develop the code for elaborating the diff command's output into something actionable for the custom needs.
Note the order of commands given to diff is : older file first arg, newer file is second arg. The "ID" column number is given in the $ID_COL global variable and which is specified on the command line. Hence it is customizable on each invocation of the command, which is what you had desired.
diff 2015.txt 2015.txt |\
perl -Mvars='%old,%new,$ID_COL,$flag' -wMstrict -F'/"(?:;")?/' -slane '
BEGIN{
open MODIFY, ">", "/tmp/log/modify_from_2015.log2" or die "$!";
open FRESH, ">", "/tmp/log/new_in_2016.log2" or die "$!";
open REMOVE, ">", "/tmp/log/removed_from_2015.log2" or die "$!";
$flag = 0;
--$ID_COL;
}
if ( !$flag && /^\D/ ) {
next; # skip state
}
if ( !$flag && /^\d/ ) { # initialization state
%old = %new = ();
$flag = 1;
next;
}
if ( $flag && s/^([<>])\s+// ) { # data state
my $line_begin = $1;
my $id = (/([^;]+)/g)[$ID_COL];
($line_begin eq ">" ? $new{$id} : $old{$id}) = $_;
redo if eof();
next;
}
if ( $flag && (/^\d/ || eof()) ) { # computation state
my($old_knt, $new_knt) = map { scalar keys %$_ } \(%old, %new);
if ( !$old_knt && !$new_knt ) {;}
elsif ( !$old_knt && $new_knt ) { print FRESH $_ for values %new; }
elsif ( $old_knt && !$new_knt ) { print REMOVE $_ for values %old; }
else {
for my $ID ( keys %new ) {
my $fh = \(exists $old{$ID} ? *MODIFY : *FRESH);
if ( exists $old{$ID} ) {
my @F_2016 = split /"(?:;")?/, $new{$ID};
my @F_2015 = split /"(?:;")?/, $old{$ID};
shift @$_ for \(@F_2016, @F_2015);
my $i = 0;
for( @F_2016 ) {
s/(.*)/\e[31m$1\e[0m/ if $F_2015[$i++] ne $_;
}
$new{$ID} = q{"} . join(q{";"}, @F_2016) . q{"};
}
print $fh $new{$ID};
delete $old{$ID};
}
print REMOVE $_ for values %old;
}
$flag=0;
last if eof();
redo;
}
END{
close MODIFY or die "$!";
close FRESH or die "$!";
close REMOVE or die "$!";
}
' -- -ID_COL=1;
HTH
-Rakesh
---In [email protected], <rachid.mokrani@...> wrote :
Hi,
diff don't return the result that i need.
Regards.
> Le 6 janv. 2016 à 17:38, 'Ruud H.G. van Tol' rvtol@... mailto:rvtol@... [sed-users] <[email protected] mailto:[email protected]> a écrit :
>
>
> On 2016-01-06 13:27, MOKRANI Rachid rachid.mokrani@... mailto:rachid.mokrani@... [sed-users]
> wrote:
>
> > I have to compare 2 files.
>
> Use diff ?
>
> -- Ruud
>
[Non-text portions of this message have been removed]
[Non-text portions of this message have been removed]