Replacing text in a file from a list in another file?

前端 未结 4 511
渐次进展
渐次进展 2020-12-21 15:18

I asked this question before but don\'t think I really explained it properly based on the answers given.

I have a file named backup.xml that is 28,000 l

4条回答
  •  悲哀的现实
    2020-12-21 15:50

    In this case you can probably get away with treating the XML as pure text. So read the XML file, and replace each occurrence of the marker with a line read from the keyword file:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use autodie qw( open);
    
    my $xml_file  = 'backup.xml';
    my $list_file = 'list.txt';
    my $out_file  = 'out.xml';  
    
    my $pattern='***';
    
    # I assumed all files are utf8 encoded
    open( my $xml,  '<:utf8', $xml_file  );
    open( my $list, '<:utf8', $list_file );
    open( my $out,  '>:utf8', $out_file  );
    
    while( <$xml>)
      { s{\Q$pattern\E}{my $kw= <$list>; chomp $kw; $kw}eg;
        print {$out} $_;
      }
    
    rename $out_file, $xml_file;
    

提交回复
热议问题