Hello all, I'm trying to parse a config with brackets file on Linux, but I want to remove commented and blank lines from input, before the actual parsing. It is a part of the project, that is written in bash and python and must run on system, that is quite old (RHEL 5, with perl-5.8.8) and limited (no internet connectivity), so I can't use modules and have to "reinvent the wheel".

At the moment I have a code that works, but is wasteful (opening and closing file twice). I first remove unwanted comments and blanks with something like this

my $data; open my $FH, '<' , $config or die "Cannot open $config: $!\n"; open my $HF, '>' , $uncommented or die "Cannot open $uncommented: $!\n +"; $data = <$FH>; while (<$FH>) { next if /^\s*#|^$/; print $HF "$_"; } close $FH; close $HF;

Or with one liner

 perl -ne 'print unless /^\s*#|^$/' config > uncommented

And then I process the uncommented file

my $data; open my $FH, '<' , $config or die "Cannot open $config: $!\n"; local $/ = undef; $data = <$FH>; while ($data =~ m/\{([^}]*)\}/gx ) { print "$1\n"; } close $FH;

I'm not able to do both on one file opening. Either regex doesn't match anything, or comments and blanks are not stripped before parsing

I understand, that the problem lies in input record separator "$/", which must be set to undef for multiline pattern match, but for per line match it must be newline (removing comments and blanks)

I wonder, if there is any elegant way how to sequentially use both while opening file only once

Thank you in advance for any tips

Yakup


In reply to Match pattern per line and after another one as multiline in single file opening by Yakup

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.