perl if line匹配正则表达式,忽略行并移动到文件中的下一行

时间:2011-06-10 15:23:56

标签: perl line ignore

如何在perl中执行以下操作:

for $line (@lines) {
    if ($line =~ m/ImportantLineNotToBeChanged/){
        #break out of the for loop, move onto the next line of the file being processed
        #start the loop again
    }
    if ($line =~ s/SUMMER/WINTER/g){
        print ".";
    }
}

更新以显示更多代码,这是我正在尝试做的事情:

sub ChangeSeason(){

    if (-f and /.log?/) {
        $file = $_;
        open FILE, $file;
        @lines = <FILE>;
        close FILE;

        for $line (@lines) {
            if ($line =~ m/'?Don't touch this line'?/) {
                last;
            }
            if ($line =~ m/'?Or this line'?/){
                last;
            }
            if ($line =~ m/'?Or this line too'?/){
                last;
            }
            if ($line +~ m/'?Or this line as well'?/){
                last;
            }
            if ($line =~ s/(WINTER)/{$1 eq 'winter' ? 'summer' : 'SUMMER'}/gie){
                print ".";
            }
        }

        print "\nSeason changed in file $_";
        open FILE, ">$file";
        print FILE @lines;
        close FILE;
    }
}

4 个答案:

答案 0 :(得分:14)

只需使用next

即可
for my $line (@lines) {
    next if ($line =~ m/ImportantLineNotToBeChanged/);
    if ($line =~ s/SUMMER/WINTER/g){
        print ".";
    }
}

答案 1 :(得分:4)

for $line (@lines) {
    unless ($line =~ m/ImportantLineNotToBeChanged/) {
        if ($line =~ s/SUMMER/WINTER/g){
            print ".";
        }
    }
}

更简洁的方法是

map { print "." if s/SUMMER/WINTER/g }
    grep {!/ImportantLineNotToBeChanged/} @lines;

(我想我做对了。)

答案 2 :(得分:4)

只需使用下一个功能。

for $line (@lines) {

  if ($line =~ m/ImportantLineNotToBeChanged/){
    #break out of the for loop, move onto the next line of the file being processed
    #start the loop again
    next;
  }
  if ($line =~ s/SUMMER/WINTER/g){
    print ".";
  }
}

同样,您可以使用“last”来完成循环。例如:

for $line (@lines) {

  if ($line =~ m/ImportantLineNotToBeChanged/){
    #continue onto the next iteration of the for loop.
    #skip everything in the rest of this iteration.
    next;
  }
  if ($line =~ m/NothingImportantAFTERThisLine/){
    #break out of the for loop completely.
    #continue to code after loop
    last;
  }
  if ($line =~ s/SUMMER/WINTER/g){
    print ".";
  }
}

#code after loop

编辑:2013年6月7日晚7点

我拿了你的代码看了看,重写了一些东西,这就是我得到的:

sub changeSeason2 {
  my $file= $_[0];

  open (FILE,"<$file");

  @lines = <FILE>;
  close FILE;

  foreach $line (@lines) {
    if ($line =~ m/'?Don't touch this line'?/) {
         next;
       }
       if ($line =~ m/'?Or this line'?/){
         next;
       }
       if ($line =~ m/'?Or this line too'?/){
         next;
       }
       if ($line =~ m/\'Or this line as well\'/){
         next;
       }
       if ($line =~ s/(WINTER)/{$1 eq 'winter' ? 'summer' : 'SUMMER'}/gie){
        print ".";
       }
  }

  print "\nSeason changed in file $file";

      open FILE, ">$file";
  print FILE @lines;
  close FILE;
}

希望这会有所帮助。

答案 3 :(得分:2)

除非我误解你,否则你的“跳出for循环,转到下一行... [然后再次启动循环”]只是一种复杂的说法“跳过这个迭代的循环体”

for $line (@lines) {
  unless (($line =~ m/ImportantLineNotToBeChanged/) {
    if ($line =~ s/SUMMER/WINTER/g) {
      print ".";
    }
  }
}