Perl:匹配后打印所有行

Jig*_*dya 4 regex perl next

我正在尝试在perl中解析文件。我想在正则表达式匹配后打印所有行

例如,文件是

num_of_dogs,10,#start_reading
num_of_cat,15
num_birds,20
num_of_butterfly,80
.....
Run Code Online (Sandbox Code Playgroud)

我要比赛后的所有台词 #start_reading

我已经尝试过了,但是只打印下一行

while (my $line = <$csv_file>) {
    next unless $line =~ /(.*),#end_of_tc/;
    if ($line =~ /(.*)/){
    print $file = $1;
    }
}
Run Code Online (Sandbox Code Playgroud)

输出看起来像这样

num_of_cats,15
num_of_birds,20
......
Run Code Online (Sandbox Code Playgroud)

提前致谢

blh*_*ing 7

您可以在行包含时设置标志,#start_reading并且仅在标志为true时才打印该行:

while (my $line = <$csv_file>) {
    print $line if $start;
    $start ||= $line =~ /#start_reading/;
}
Run Code Online (Sandbox Code Playgroud)

如果您想在遇到#stop_reading以下情况后停止阅读:

while (my $line = <$csv_file>) {
    print $line if $print;
    $print ||= $line =~ /#start_reading/;
    $print &&= $line !~ /#stop_reading/;
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*ley 7

您还可以使用触发器(..)运算符绕过从文件开头到包含该行的所有行。#start_reading

while (<$fh>) {
    next if 1 .. /#start_reading/;
    print;
}
Run Code Online (Sandbox Code Playgroud)

这将绕过从文件第1行到匹配行的打印#start_reading。然后,它将打印文件中的其余行。