加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 23:30:34 所属栏目:大数据 来源:网络整理
导读:你会如何在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
你会如何在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 ".";
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读