Yakup has asked for the wisdom of the Perl Monks concerning the following question:
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 > uncommentedAnd 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
|
|---|