Hello, I hope someone can help me with some ideas on this.

Quite often I end up working with big text files (~500k lines) which have configuration data that I want to change using a Perl program. The data files often don't have any official format. The structure of these kind of files are often similar and the content could look something like the following examples:

#ObjectType1 Param1: 8 Param2: SomeText #ObjectType1.NestedObject Param1: 3 Param2: SomeText #ObjectType1 ... #ObjectType2 ...

or

ObjectType1 { Param1 = 8 Param2 = SomeText NestedObject { Param1 = 3 Param2 = SomeText } } ObjectType2 { ... } ObjectType1 { ... }

Most of the time I want to do something like changing the values of parameters for a certain object type and leave all the other lines inside the data file 'untouched'. A very simplistic approach that I used looks like the next code example (second data example). It reads the file line by line and keeps track of which 'context' it is currently reading and acts depending on that context. It works fine (as long as the format does not change too much), however the more complex things that I want to do these kind of snippets tend to become very complex and difficult to maintain.

use strict ; use warnings ; my $file = "test" ; open (my $fhi, "<", $file . ".dat" ) or die "Cannot open $file.dat\n" +; open (my $fho, ">", $file . "_out.dat" ) or die "Cannot open $file" . +"_out.dat\n" ; my $context = "" ; while ( my $line = <$fhi> ) { chomp $line ; if ( $line =~ /ObjectType1/ ) { $context = "ObjectType1" ; } if ( $line =~ /$\}/ ) { $context = "" ; } if ( $context eq "ObjectType1" ) { if ( $line =~ /Param1/ ) { print $fho "Param1 = 0\n" ; } elsif ( $line =~ /Param2/ ) { print $fho "Param2 = SomeOtherText\n" ; } else { print $fho $line . "\n" ; } } else { print $fho $line . "\n" ; } }

Does anyone know of a better or more generic way to do these kind of things? I am looking for a very simple approach (search and replace, not reading the entire data file to memory) where I can flexibly define a formula that is applied to a parameter within the scope of the context it is in.

Thanks, Veltro

edit:/\}/ => /$\}/


In reply to Contextual find and replace large config file by Veltro

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.