简体   繁体   中英

Compare and replace two files in perl

I am trying to compare the string in two documents test1, test 2

Test 1:

 <p><imagedata rid="rId7"></p>
  ...
 <p><imagedata rid="rId8"></p>

Test2:

<imagesource Id="rId7" Target="image/image1.jpg"/>
...
<imagesource Id="rId9" Target="image/image2.jpg"/>
...
<imagesource Id="rId8" Target="image/image3.jpg"/>

What I want is, the first file should get replaced with the image target path like:

 <p><imagedata src="image/image1.jpg"></p>
  ...
 <p><imagedata rid="image/image3.jpg"></p>

I tried to extract the text from both files but I stuck to compare both strings

 opendir(DIR, $filenamenew1);

 our(@test1,@test2);

 open fhr, "$filenamenew1/test1.txt";

 open fhr1, "$filenamenew1/test2.txt";


 my @line;

 @line= <fhr>;

 for (my $i=0;$i<=$#line;$i++)
 {
 if ($line[$i]=~m/rid="(rId[0-9])"/)
 {
 my $k = $1;

 push (@test1, "$k");
 }
 }


 my @file2;

 @file2= <fhr1>;

 for (my $i=0;$i<=$#file2;$i++)
 {
 if ($file2[$i]=~m/Id="(rId[0-9])"/)
 {
 my $k1 = $1;

 push (@test2, "$k1");


 foreach (@test1 = @test2)
 {
 print "equal";
 }

 }

 }

One solution could be to read first the file with <imagesources> and save both the rid and the target in a hash. After that read the other file line by line and compare if the rid exists in the hash and do the substitution, something like:

Content of script.pl :

#!/usr/bin/env perl

use warnings;
use strict;

my (%hash);

open my $fh2, '<', shift or die;
open my $fh1, '<', shift or die;

while ( <$fh2> ) {
        chomp;
        if ( m/Id="(rId\d+)".*Target="([^"]*)"/i ) {
                $hash{ $1 } = $2;
        }
}

while ( <$fh1> ) {
        if ( m/rId="([^"]+)"/i && defined $hash{ $1 } ) {
                s//src="$hash{ $1 }"/;
        }
        print $_;
}

Run it like:

perl script.pl test2 test1

That yields:

<p><imagedata src="image/image1.jpg"></p>
 ...
<p><imagedata src="image/image3.jpg"></p>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM