I am trying to write a Perl script to parse a file and segregate its contents based on a match criteria. A sample file (for e.g. test.txt) would look like something:
command something something keyword something something
filepath1
filepath2
command something something something something
filepath3
filepath4
filepath5
The output of the script would be two files based on the name of input file for the script, test.keyword and test.nomatch.
test.keyword would have something like this:
command something something keyword something something
filepath1
filepath2
test.nomatch would have something like this:
command something something something something
filepath3
filepath4
filepath5
I have tried searching for ways of achieving this but I am unable to find something that helps me. This is the only part of my script which is left and has me going crazy now. And yes, I am not an expert in Perl. :(
Following is the skeleton that I have at the moment which is waiting for that golden loop condition:
#!/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);