我正在尝试编写一个 Perl 脚本来解析文件并根据匹配条件分离其内容。一个示例文件(例如 test.txt)看起来像这样:
command something something keyword something something
filepath1
filepath2
command something something something something
filepath3
filepath4
filepath5
脚本的输出将是基于脚本输入文件名的两个文件,test.keyword 和 test.nomatch。
test.keyword 会是这样的:
command something something keyword something something
filepath1
filepath2
test.nomatch 会是这样的:
command something something something something
filepath3
filepath4
filepath5
我已经尝试寻找实现这一目标的方法,但我找不到对我有帮助的东西。这是我剧本中唯一剩下的部分,现在让我发疯了。是的,我不是 Perl 专家。:(
以下是我目前正在等待黄金循环条件的骨架:
#!/usr/bin/perl -a
my $fh = shift or die "No file passed as argument for splitting!\n";
open( infile, "<$fh" ) or die $!;
open( vef_output, ">$fh.vef" ) or die $!;
open( svf_output, ">$fh.svf" ) or die $!;
$mode = 0; #0 for vef and 1 for svf
while ( my $line = <$infile> ) {
if ( $line =~ /SOME_LIBRARY_OPTIONS/ ) {
if ( $line =~ /\-sv/ ) {
print {$svf_output} $line;
$mode = 1;
}
else {
print {$vef_output} $line;
$mode = 0;
}
next;
}
if ( $mode eq 0 ) {
print {$vef_output} $line;
next;
}
else {
print {$svf_output} $line;
next;
}
}
close($vef_output);
close($svf_output);
close($file);