开发者

Grabbing lines after a matched line in perl

开发者 https://www.devze.com 2023-03-23 11:40 出处:网络
How do I access all lines after the line on which a pattern matched occur For example BCDA ABCD AAAABBBBCCCCDDD

How do I access all lines after the line on which a pattern matched occur For example

BCDA

ABCD

AAAABBBBCCCCDDD

AAAAAABBBBBBCCC

AAAAAAAAAAAAAA

So basically after the pattern ABCD is matched i want to process all the lines after it.Put it in a开发者_运维技巧n array.So do a pattern match only once .


This is the simplest example I can think of for what it sounds like you are looking for. It puts "all the lines after " the line that matched into an array.

my @lines;
while ( <$in> ) { 
    next unless m/ABCD/;
    # in an list context, this will slurp the rest of the file.
    @lines = <$in>;
}


A bit unclear, but is this what you are after?

The range operator is ideal for this sort of task:

#!/usr/bin/perl

my @array;

while (<DATA>)  {
  push @array, $_ if /ABCD/ .. 0
}
shift(@array);

print @array;

__DATA__
BCDA
ABCD
AAAABBBBCCCCDDD
AAAAAABBBBBBCCC
AAAAAAAAAAAAAA

Outputs:

AAAABBBBCCCCDDD
AAAAAABBBBBBCCC
AAAAAAAAAAAAAA
0

精彩评论

暂无评论...
验证码 换一张
取 消