Perl 如何跳过文件的行,只检索所需的中间行?

Perl 如何跳过文件的行,只检索所需的中间行?,perl,Perl,我有一个txt文件,如下所示: The number of lines on the upper part of this file can vary in size . : . : . : . : . : . : . lineA lineB lineC _____________________________________________________________ The number of lines on

我有一个txt文件,如下所示:

The number of lines
on the upper part of 
this file can 
vary in size
.    :    .    :    .    :    .    :    .    :    .    :    .
lineA
lineB
lineC
_____________________________________________________________
The number of lines
on the lower part of 
this file can 
also vary in size
我想抓住所有的线之间

.    :    .    :    .    :    .    :    .    :    .    :    .

我想忽略上面和下面的其他行。我尝试了下一个,直到最后都没有成功。关于如何做到这一点有什么想法吗?这就是我目前所拥有的

open (IN, "$file") || die "Cannot open file $file";
while(my $line=<IN>) 
{
    chomp $line;
    last if ($line =~ m/:/);
    #do something with these lines
}
你可以用。。或操作员通过给出开始和结束条件来过滤线路

例如:

#!/usr/bin/perl

use strict;
use warnings;

while (<DATA>) {
    if (m/^\./ ... m/^_/) {
            # You may want to do more filtering at here
        print;
    } 
}

__DATA__
The number of lines
on the upper part of 
this file can 
vary in size
.    :    .    :    .    :    .    :    .    :    .    :    .
lineA
lineB
lineC
_____________________________________________________________
The number of lines
on the lower part of 
this file can 
also vary in size
你可以用。。或操作员通过给出开始和结束条件来过滤线路

例如:

#!/usr/bin/perl

use strict;
use warnings;

while (<DATA>) {
    if (m/^\./ ... m/^_/) {
            # You may want to do more filtering at here
        print;
    } 
}

__DATA__
The number of lines
on the upper part of 
this file can 
vary in size
.    :    .    :    .    :    .    :    .    :    .    :    .
lineA
lineB
lineC
_____________________________________________________________
The number of lines
on the lower part of 
this file can 
also vary in size
因此,我们希望所有行都在1之后,并在匹配/E时结束while循环/


因此,我们希望所有行都在1之后,并在匹配/E/

next if时结束while循环$行=~m/:/;下一个如果$行=~m/:/;谢谢,这很有效。但是,如果您能向我解释下一个if和最后一个if语句,以便我将来可以再次使用它,那就太好了。我更希望看到next,除非$switch和$switch>1,而不是强制所有假值为零。您的表达式所做的事情并不明显,它确实需要注释,因此OP要求解释code@Borodin检查解释是下面的代码,除非可以混淆一些。谢谢,这是伟大的作品。但是,如果您能向我解释下一个if和最后一个if语句,以便我将来可以再次使用它,那就太好了。我更希望看到next,除非$switch和$switch>1,而不是强制所有假值为零。您的表达式所做的事情并不明显,它确实需要注释,因此OP要求解释code@Borodin检查下面的解释代码,除非可以混淆一些。
$ perl t.pl
.    :    .    :    .    :    .    :    .    :    .    :    .
lineA
lineB
lineC
_____________________________________________________________
use strict;
use warnings;

open (my $IN, "<", $file) || die $!;
while (my $line = <$IN>)
{
    chomp $line;
    # force 0 instead of empty string 
    # so it can be used to numerically compare under warnings
    my $switch = ($line =~ /:/ .. $line =~ /__/) ||0;

    next if $switch <=1;
    last if $switch =~ /E/;

    #do something with these lines

}
1   # starting with /:/ line
2   # lineA
3   # lineB
4   # lineC
5E0 # ending with /__/ line