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

前端之家收集整理的这篇文章主要介绍了perl if line匹配正则表达式,忽略行并移动到文件中的下一行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
你会如何在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;
    }
}

解决方法

只需使用 next

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

猜你在找的Perl相关文章