文件已
sub TC1
{
check(CLEANUP_SUB=>\&cleanup1);
check(CLEANUP_SUB=>\&cleanup2);
check(CLEANUP_SUB=>\&cleanup3);
endcase
}
sub TC2
{
check(CLEANUP_SUB=>\&cleanup4);
check(CLEANUP_SUB=>\&cleanup5);
check(CLEANUP_SUB=>\&cleanup6);
endcase();
}
\&cleanup1
\&cleanup2
\&cleanup3
我想要/ sub TC1 /之后的所有模式,直到/ endcase /
的第一次出现perl -0777 -ne 'if(/sub\sTC1/){while(/CLEANUP_SUB\s*=>([^),;]+)/g){print "$&\n";}}' file
\&cleanup1
\&cleanup2
\&cleanup3
\&cleanup4
\&cleanup5
\&cleanup6
答案 0 :(得分:1)
使用触发器运算符(..
)。
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
while (<>) {
if (/sub TC1/ .. /endcase/) {
/CLEANUP_SUB\s*=>([^),;]+)/ and say $1;
}
}
或者(单线):
$ perl -nE '/sub TC1/ .. /endcase/ and /CLEANUP_SUB\s*=>([^),;]+)/ and say $1' file