Perl: How to print next line after matching a pattern?

后端 未结 7 1951
刺人心
刺人心 2021-01-06 15:52

I would like to print specific data after matching a pattern or line. I have a file like this:

#******************************    
List : car  
Design: S           


        
7条回答
  •  天涯浪人
    2021-01-06 15:54

    I got your code to work with a couple small tweaks. It's still not perfect but it works.

    • "while" should be lower case.
    • You never increment $i.
    • The way you reuse @array is confusing at best, but if you just output $a you'll get your car data.

    Code:

    $file_to_get = "input_file.txt";
    open (FILE, $file_to_get) or die $!;
    
    my @array;
    
    while (@array = ) {
        $i = 0;
    
        foreach my $line (@array) {
    
            if ($line =~ m/(Car)((.*))/) {
                my $a = $array[$i+2];
                push (@array, $a);
                print $a;
            }
    
            $i++;
        }
    }
    close(FILE);
    

提交回复
热议问题