in reply to Newbie Question on parsing files
One of the best pieces of general advice we can give you is use strictures: use strict; use warnings;. They often pick up issues before they become issues.
A cute trick that Perl can do is use a different "line terminator" than the default by setting the $/ special variable. So you can:
my @paras = do {local $/ = "\n\n"; <FILE>};
which locally (to the do) overrides the line end terminator to be "\n\n", then uses <FILE> in an array context (my @paras = is array context) to "slurp" the file a paragraph at a time into @paras.
You can then use grep to filter out all the paragraphs that don't start with the keyword:
my @wanted = grep {/^\Q$KWD:\E/} @paras;
which uses a regular expression (see perlretut for a start) to check that the paragraph starts with the required key word. Now you can print the result out:
print OUTFILE @wanted;
|
|---|