Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Match pattern per line and after another one as multiline in single file opening

by Marshall (Canon)
on Feb 15, 2017 at 00:31 UTC ( [id://1182036]=note: print w/replies, xml ) Need Help??


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

You say: I'm trying to parse a config with brackets file on Linux ,but you do not present an example file.

It would be extremely helpful to show an example config file and also explain what information you are trying to get out of it?

Generating a single string from the config file, which is then parsed, may or may not be appropriate. Parsing line by line may or may not be more appropriate.

Below I demo a rather generic algorithm for parsing the sections within the file line by line. Without any spec, I just made up data for the input file. This code can be adapted to process a "generic {} file". The code allows for the idea of a "root" unnamed {section}.

Do not mistake brevity for efficiency. Also, I have no idea what you want to do with the data within the {config} sections.

There are of course many methods to implement this type of code.

#!usr/bin/perl use strict; use warnings; use Data::Dumper; my %section_lines; while (my $line = <DATA>) { next if $line =~ /^\s*$/; # skip blank lines next if $line =~ /^\s*#/; # skip comments chomp $line; if ($line =~ m/\{([^}]*)\}/) { process_section($1); } else { # Could be in a "root" un-named section? Or # Could be some junk, not a comment, not a line within # a section? Shouldn't happen, but maybe it does? print STDERR "Skipping Illegal line: \'$line\'\n"; } } sub process_section { my $section_name = shift; # allow for a blank section data (no lines within it) # the existence of such a thing could have meaning? $section_lines{$section_name} = [] if (!$section_lines{$section_na +me}); while (my $line = <DATA>) { next if $line =~ /^\s*$/; # skip blank lines ?? next if $line =~ /^\s*#/; # skip comments ?? if ($line =~ m/\{([^}]*)\}/) # new section detected... { process_section($1); } else { $line =~ s/^\s*//; # trim leading space $line =~ s/\s*$//; # trim trailing space (inc EOL) # I have no idea of what processing is needed here. # This just adds a line to the section that is # being parsed. push @{$section_lines{$section_name}}, $line; } } } print Dumper \%section_lines; =This Program Prints: Skipping Illegal line: 'this a bogus line, not in a named section' Skipping Illegal line: 'is there a "root" un-named section possible?' $VAR1 = { 'section 2' => [], 'section 3' => [ 'xyzzy = 57', 'this line might mean something?' ], 'section 1' => [ 'a =2', 'b =something' ] }; =cut __DATA__ # Please show a "real" file here, just a guess... # this is comment this a bogus line, not in a named section is there a "root" un-named section possible? {section 1} a =2 b =something {section 2} # some comment embedded in section {section 3} # comment xyzzy = 57 this line might mean something?
  • Comment on Re: Match pattern per line and after another one as multiline in single file opening
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1182036]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found