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?

In reply to Re: Match pattern per line and after another one as multiline in single file opening by Marshall
in thread 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.