如何用awk搜索和替换这些记录

时间:2015-05-14 10:39:51

标签: perl awk

我有以下文件:

     %0 Book
     %T Gale encyclopedia of medicine
     %A Deirdre S. Blanchfield
     %A Jacqueline L. Longe
     %A Gale Research Company
     %@ 0787654892
     %D 2002
     %I Gale Group 
     %R 10.1001/0787654892
     %F Gale_Thomson_Encyclopedias/Medicine and Health/0787654892/Gale Encyclopedia of Medicine. Vol. 1. 2nd ed.pdf

     %0 Book
     %T Encyclopedia of United States national security
     %A Richard J. Samuels
     %@ 0761929274
     %D 2006
     %I SAGE 
     %R 10.1001/0761929274
     %F Gale_Thomson_Encyclopedias/Politics, Law, Sociology/Encyclopedia of United States National Security_v1-2_0761929274 (Sage, 2006).pdf

     %0 Book
     %T Gale encyclopedia of medicine
     %A Deirdre S. Blanchfield
     %A Jacqueline L. Longe
     %A Gale Research Company
     %@ 0787654892
     %D 2002
     %I Gale Group 
     %R 10.1001/0787654892
     %F Gale_Thomson_Encyclopedias/Medicine and Health/0787654892/Gale Encyclopedia of Medicine. Vol. 2. 2nd ed.pdf

并希望在序列中使用%F中的文件名替换文件。

    /ModDate(D:

    /Subject (??? 2015, :. doi:<content of field %R>')/ModDate(D:

在上面的情况下(显示第一条记录的onyl,但应该对所有记录进行):

名称为

的文件
     Gale_Thomson_Encyclopedias/Medicine and Health/0787654892/Gale Encyclopedia of Medicine. Vol. 1. 2nd ed.pdf

以下顺序:

      /ModDate(D:  

替换为

      /Subject (??? 2015, :. doi:10.1001/0787654892)/ModDate(D:

如何用awk完成?非常感谢!如果文件中已有/subject ( .. )行,则可能会出现问题。在替换完成之前应该删除它。

欢迎任何帮助。

1 个答案:

答案 0 :(得分:0)

我试过以下:看起来不错:

     #!/usr/bin/perl

     use strict;
     use warnings;

     #my $mypdf = "";
     #my $mydoi = "";
     #my @rest;
     #my @rest2;
     my $pdfbuf;
     my $doibuf;

     while (<>) {
         chomp;

         #($mydoi, $mypdf)  = m/^%R (.*)|^%F (.+)/g;

         my  ( $mydoi, @rest) = m/^%R (.*)/g ;
            #print '->' . $mydoi if ($mydoi);
            $doibuf = $mydoi if ($mydoi);

         my  ( $mypdf, @rest2) = m/^%F (.*)/g ;
            #print '->' . $mypdf if ($mypdf);
            $pdfbuf = $mypdf if ($mypdf);


         #my ( $mydoi, @rest2 ) = m/^%R (.*)/g;


         if (($pdfbuf)&&($doibuf)) 
            {

               #print  "NOW: ",$doibuf,", and "   ;
               #print  $pdfbuf, "\n"  ;
               $doibuf =~ s/\//\\\//g;
               $pdfbuf =~ s/ //g;
               my $subj   =     '/Subject (??? 2015, :. doi:'.$doibuf.')' ;
               my $search =   '(/ModDate.D.*?\))';

               #my $cmd = 'awk -v add=\'' . $subj . '\' \'1; /' . $search . '/{print add}\'   ./' . $pdfbuf . ' >  ./' . $pdfbuf . '_withDOI' ;
               my $cmd = ' perl -pe \'s|' . $search . '|\1' . $subj . '|\'   ./' . $pdfbuf . ' >  ./' . $pdfbuf . '_withDOI' ;
               my $outputofshell = qx/$cmd/;

               print $cmd, "\n" ; 
               $doibuf=""; $pdfbuf="";


            }


     }