Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl if line matches regex, ignore line and move onto next line in file

Tags:

perl

line

How would you do the following in 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 ".";
    }
}

Updated to show more code, this is what I'm trying to do:

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;
    }
}
like image 734
ar.dll Avatar asked Jun 10 '11 15:06

ar.dll


1 Answers

Just use next

for my $line (@lines) {
    next if ($line =~ m/ImportantLineNotToBeChanged/);
    if ($line =~ s/SUMMER/WINTER/g){
        print ".";
    }
}
like image 87
Toto Avatar answered Oct 29 '22 14:10

Toto