Thrown a problem at work to edit a customer configuration file. Though I've whipped up something that seems to work, my Perl has become quite rusty, so I seek suggestions for better ways to do it.

Though the precise syntax of this configuration file is unknown, I've got an example configuration file, shown below. One approach is write a formal parser. The other extreme is to hack out some regexes. I've taken a middle ground, crudely parsing the VIEW records by matching with \G in harness with the /gc regex match modifier, then applying substitution regexes to modify each VIEW record.

To keep the sample code smallish, assume the goal is to change field values of "REGION" to "LOCATION" and "RUBBISH" to "TRASH" but only if the RECORD value in the VIEW is either "ABC" or "XYZ". The test program is shown below:

# ptest.pl # # Sample program to edit a configuration file. # This configuration file lacks a formal syntax specification; # the example configuration below is all I've got. # All fields should be treated case insensitively. # The goal is to change field values of: # "REGION" to "LOCATION" # "RUBBISH" to "TRASH" # but only if the RECORD value in the VIEW is either "ABC" or "XYZ". # If you run with: # perl ptest.pl >1.tmp 2>2.tmp # 1.tmp will contain original file, 2.tmp will contain the changed ver +sion. use strict; use warnings; my $s_in = <<'GROK'; # comment line VIEW View1 RECORD "ABC" FIELD ( FIELD "TYPEFROM" FIELD "REGION" FIELD "RUBBISH" ) INTERVAL 600 SECONDS END_VIEW VIEW View2 RECORD "HELLO" FIELD ( FIELD "TYPETO" FIELD "REGION" FIELD "RUBBISH" ) INTERVAL 700 SECONDS END_VIEW # random line 1 VIEW View3 RECORD "XYZ" FIELD ( FIELD "FLD1" FIELD "Region" # random line 2 FIELD "Rubbish" ) INTERVAL 800 SECONDS END_VIEW # random line 3 GROK # Ensure properly newline terminated substr( $s_in, -1 ) ne "\n" and $s_in .= "\n"; my @recs = ( 'ABC', 'XYZ' ); my %fldmap = ( REGION => 'LOCATION', RUBBISH => 'TRASH', ); my $fldstr = join '|', keys %fldmap; print $s_in; my $s_out; while (1) { # Extract VIEW ... END_VIEW block if ( $s_in =~ /\G(^[ \t]*\bVIEW\b.*?^[ \t]*\bEND_VIEW\b)/msgic ) { my $view = $1; # Check for matching RECORD in VIEW block if ( $view =~ /^[ \t]*\bRECORD\b[ \t]*"(.*?)"/mi ) { my $rec = $1; if ( length($rec) && grep( /^\Q$rec\E$/i, @recs ) ) { # Translate fields $view =~ s/(\bFIELD\b[ \t]*?")($fldstr)"/$1 . $fldmap{uc($ +2)} . '"'/gie; } } $s_out .= $view; } elsif ( $s_in =~ /\G(.*\n)/gc ) { $s_out .= $1; } else { last; } } warn $s_out;

Updated: Minor changes made to code: ensured input file properly newline terminated plus very minor tweaks to regex.


In reply to Parsing and editing a configuration file by eyepopslikeamosquito

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.