perl& amp;中的模式匹配分配

时间:2015-07-29 04:20:39

标签: perl

我需要在文件中搜索特定字符串,然后将其分配给变量,例如:在文件内容中,它被写为CURRENT_RUN_ID=1636,因此我需要搜索字符串CURRENT_RUN_ID并将其给定的值(即1636)分配给a variable say runvar`,为此我尝试下面给出,但它似乎没有用,你能在这里纠正我吗?

opendir(DAPATH,$sDAPATH) or die "Can't open $sDAPATH: $!"; 
print OUTLOG "\nfound da path : $sDAPATH\n"; 
my @adirs = readdir(DAPATH); 
print  OUTLOG "Starting capturing DA\n";
my $da = glob "*$runVar.csv*";
print  OUTLOG "Assigned DA";
closedir(DAPATH);
}

2 个答案:

答案 0 :(得分:1)

它不匹配,因为你在匹配中给CURRENT_RUN_ID = 1636处的空格。它正在搜索字符串中的空格。

您要匹配的模式将与CURRENT_RUN_ID字符串匹配,请注意==之间以及CURRENT_RUN_ID之后的空格。如果匹配则模式中的空格的数量应与字符串中的空格的数量完全相同。

==之间以及\s*之后还有空格。 最好使用if (/CURRENT_RUN_ID\s*=\s*(.*)/){ my $runvar = $1; print "$runvar \n"; } 删除有缺陷的空格并使空格可选,请尝试:

#!/usr/bin/perl
use strict;
use warnings;
open my $fh, '<', 'file' or die "unable to open file : $! \n";
my $runVar="";
while(<$fh>){
      if (/CURRENT_RUN_ID\s*=\s*(.*)/){
        print  "we can now assign run id\n";
        $runVar = $1;
        print   "assigned current run id to variable\n";
        }
       else {
       print  "run id not assigned\n";
      }
}
close($fh);

编辑:

根据您的要求,我将脚本更改为(我不写入文件):

<lineMode width="2" dash="5,5,2,10"/>
<lines>5.5in 4.5in 6.5in 4.5in
    6.5in 4.5in 6in 4in
    6in 4in 6in 4.75in
</lines>
<drawCentredString x="6 in" y="3.75 in">
    width=2, dash=5,5,2,10
</drawCentredString>

答案 1 :(得分:0)

在第18行执行模式匹配时,您不匹配任何行。甚至您可以减少代码中的重复步骤。 但为方便起见,我使用了你的程序。

#!C:\Strawberry\perl\bin
use strict;
use warnings;
my $sSuccessString = "CURRENT_RUN_ID"; 
open(LOG,"$slogfile") or die("Can't open $slogfile\n");
my $sLines;
{
local $/ = undef;
$sLines=<LOG>;
}
    if($sLines =~ m/$sSuccessString/){
    open(OUTLOG, ">>test.txt");
    print OUTLOG "found current run id in log\n";
    print OUTLOG "it is found in log as : $sSuccessString \n";
    #if ($sLines =~ m/(CURRENT_RUN_ID=.*)/i) {
    #print OUTLOG "<p>" . $1 . "<\/p>\n";
 #In below line you need to match the pattern with the line.
    if ($sLines=~/CURRENT_RUN_ID=(.*)/){   
    print OUTLOG "we can now assign run id\n";
    my $runVar = $1;
    print "$runVar\n";      
    print OUTLOG  "assigned current run id to variable\n";
    }
    else {
    print "run id not assigned\n";
    }
    }

我已将您的日志文件内容复制到我桌面上的一个文本文件(prog.txt)并运行该脚本。请参阅输出。

 #!C:\Strawberry\perl\bin
 use strict;
 use warnings;
 my $sSuccessString = "CURRENT_RUN_ID"; 
 open(LOG,"Prog.txt") or die("Can't open text file\n");
 my $sLines;
 {
 local $/ = undef;
 $sLines=<LOG>;
 }
    if($sLines =~ m/$sSuccessString/){
    open(OUTLOG, ">>test.txt");
    print OUTLOG "found current run id in log\n";
    print OUTLOG "it is found in log as : $sSuccessString \n";
    #if ($sLines =~ m/(CURRENT_RUN_ID=.*)/i) {
    #print OUTLOG "<p>" . $1 . "<\/p>\n";
    if ($sLines=~/CURRENT_RUN_ID=(.*)/){
    print OUTLOG "we can now assign run id\n";
    my $runVar = $1;
    print "$runVar\n";      
    print OUTLOG  "assigned current run id to variable\n";
    }
    else {
    print "run id not assigned\n";
    }
    }

<强>输出
      C:\ Users \ hclabv \ Desktop&gt; perl run1.pl

1637

我在运行脚本时捕获日志的日志文件(test.txt)如下所示。

found current run id in log
it is found in log as : CURRENT_RUN_ID 
we can now assign run id
assigned current run id to variable

为了从所需目录获取runVar.csv文件,我在if循环中添加了下面的代码

opendir(DAPATH,"C:/Users/hclabv/Desktop/Scripting/files") or die "Can't open    DAPATH: $!"; 
    print OUTLOG "\nfound da path : DAPATH\n"; 
    my @adirs = readdir(DAPATH);
    print "csv files @adirs\n"; 
    print  OUTLOG "Starting capturing DA\n";
    my $csvfile = "$runVar.csv";
    foreach my $adirs (@adirs) {
          if ($adirs eq $csvfile) {
          print "file found\n";
          my $da = $adirs;      
          print "csv file is $da\n";
    }
    print  OUTLOG "Assigned DA";
    close(DAPATH); 
    }