在文件中剪切并粘贴特定的字符串

时间:2019-07-11 07:56:24

标签: perl

在文件中,我需要剪切文本(以\qr{....text}的形式)并放置 在两次回车之后。

输入文件文本为:

\section{bla bla ...\qr{text} bla bla..}

Some paragraph text......

必需的输出:

\section{bla bla bla bla}

\qr{text}......
Some paragraph text 

我的尝试

use strict;
use warnings;

my $InputXmlFile = $ARGV[0];
my $OutputXmlFile = $InputXmlFile;
my $OutProcesText = "";
$OutputXmlFile =~ s#\.tex$#\.tex#gsi;
my $workingpath = $1 if $InputXmlFile =~ m/(.*)\\(.+)\.tex/i;
my $filename = $2 if $InputXmlFile =~ m/(.*)\\(.+)\.tex/i;

open (INTXT, "$InputXmlFile");
my $inText = join("",<INTXT>);
close (INTXT);

while($inText =~ m#\\section\{(.*?)\}\n\n#gi) {
    my $qrtext = $1;
    if ($qrtext =~s/\\qr\{(.*?)\}//gi) {
        my $qrFindText = $1;
        $qrtext =~ s/\x0D\x0D/\x0D\x0D$qrFindText/g; 
        print $qrFindText;
    }
    open (FIH,'>',$OutputXmlFile);
    print FIH $inText;
    close(FIH);
}

2 个答案:

答案 0 :(得分:0)

请尝试以下操作:

use strict;
use warnings;

my $brc = qw/((?:[^{}]*(?:{(?:[^{}]*(?:{(?:[^{}]*(?:{[^{}]*})*[^{}]*)*})*[^{}]*)*})*[^{}]*)*)/;
my $InputXmlFile = $ARGV[0]; (my $OutputXmlFile = $InputXmlFile)=~s/\.tex$/\.newtex/i;

open(INTXT, $InputXmlFile) || die "Couldn't able to read: $!\n";
my $inText = join("",<INTXT>);
close(INTXT);

my ($beh,$match,$byn) = "";
while($inText=~m/\\section\{$brc\}/gs)
{
    $beh = $beh.$`; $match = $&; $byn = $'; my $qrt = $1;
    if($qrt=~m/\\qr{$brc}/i) { my $qrtex = $&; $match=~s/\\qr\{$brc\}//; $match=~s/$/\n\n$qrtex/; }
    $beh = $beh.$match; $inText = $byn;
}
if(length $beh) {  $inText = $beh.$byn;  }

open(FIH,'>',$OutputXmlFile);
print FIH $inText;
close(FIH);

答案 1 :(得分:0)

   use strict;
   use warnings;
   use XML::Simple;

   my $path = $ARGV[0];
   my $prePath = "d:\\Process\\Latex\\app\\";
   $path =~ s/(.*?)\/(.*?)\/(.*?)\/(.*?)\/(.*?)\/(.*?)\.tex/$5/g;
   my $artID = $5;
   $prePath= $prePath. $path. "\\ms\\"; 
   my $authorData;
   my @authorData;

   &authorXml;

   sub authorXml{
   my $InputXmlFile = $ARGV[0]; (my $OutputXmlFile =$InputXmlFile)=~s/\.tex$/\.tex/i;
      open(INTXT, $InputXmlFile) || die "Couldn't able to read: $!\n";
      my $inText = join("",<INTXT>);
      close(INTXT);
      $inText =~ s/\%\\def\\authoryellow\{\}//gi;
      while ($inText =~ s/\\author(.*?)\n//){
         $authorData = $1;
         push @authorData,$authorData;
      }
      foreach my $audata(@authorData){
          print $audata."\n";
      }
      open (DATA, "<", "$prePath/$path.xml") or die "Not able to open the file, $!";
      my $cats_xml = <DATA>;
      close DATA;
      $cats_xml=$text=~/\author\[(.*?)\]\{(.*?)\}\n/g;
      print $xmlF;
   }
      open(FIH,'>',$OutputXmlFile);
      print FIH $inText;
      close(FIH);
   }