Limo has asked for the wisdom of the Perl Monks concerning the following question:

2 remaining problems: The input file:


version "4.0I0 tlim";
system {
    host-name steve-1;
    domain-search  mydomain.com mydomain.net ;
    time-zone Here/There;
    authentication-order  plus password ;
    root-authentication {
#        encrypted-password "CENSORED"; # CENSORED-SECRET-DATA
    }
    name-server {
        0.0.0.0;
        1.1.1.1;
    }
    plus-server {
        3.3.3.3 {
#            secret  "CENSORED"; # CENSORED-SECRET-DATA
            timeout 20;
        }
        4.4.4.4 {
#            secret  "CENSORED"; # CENSORED-SECRET-DATA
            timeout 20;
        }
    }
    login {
        class super-user {
            idle-timeout 120;
            permissions all;
        }
        user local {
            uid 5555;
            class super-user;
            authentication {
#                encrypted-password "CENSORED"; # CENSORED-SECRET-DATA
            }
        }
        user remote {
            uid 2004;
            class super-user;
        }
    }
    services {
        telnet;
    }
    syslog {
        archive size 10000000 files 10;
        user * {
            any emergency;
        }
        host 6.6.6.6 {
            authorization any;
            kernel any;
            user any;
            change-log any;
            facility-override local7;
        }
        host 7.7.7.7 {
            authorization any;
            kernel any;
            user any;
            change-log any;
            facility-override local7;
 


yields:

version "4.0I0 tlim";
system host-name 1steve-1;
system domain-search  mydomain.com mydomain.net ;
system time-zone Here/There;
system authentication-order  plus password ;
system root-authentication #        encrypted-password "CENSORED"; # CENSORED-SECRET-DATA
system name-server 0.0.0.0;
system name-server 1.1.1.1;
system plus-server 3.3.3.3 #            secret  "CENSORED"; # CENSORED-SECRET-DATA
system plus-server 3.3.3.3 timeout 20;
system plus-server 4.4.4.4 #            secret  "CENSORED"; # CENSORED-SECRET-DATA
system plus-server 4.4.4.4 timeout 20;
system login class super-user {
system login idle-timeout 120;
system login permissions all;
system user local {
system uid 5555;
system class super-user;
system authentication #                encrypted-password "CENSORED"; # CENSORED-SECRET-DATA
user remote {
uid 2004;
class super-user;
services telnet;
syslog archive size 10000000 files 10;
syslog user * {
syslog any emergency;
host 6.6.6.6 {
authorization any;
kernel any;
user any;
change-log any;
facility-override local7;
host 7.7.7.7 {
authorization any;
kernel any;
user any;
change-log any;
facility-override local7;


The remaining problems: 1. deleting whitespace eg:

system authentication #                encrypted-password "CENSORED"; # CENSORED-SECRET-DATA

to:

system authentication # encrypted-password "CENSORED"; # CENSORED-SECRET-DATA

problem 2:

...........snip.......

system authentication #                encrypted-password "CENSORED"; # CENSORED-SECRET-DATA
user remote {
uid 2004;
...etc.

I lose the prepending strings here, and I don't know why. I suspect that it has to do with multiple "}"'s Thanks for all your help thus far......

Replies are listed 'Best First'.
Re: 2 remaining problems (regex)
by isotope (Deacon) on Aug 25, 2000 at 02:09 UTC
    Trimming the extra whitespace can be done with this regexp:
    s/\s+/ /g;

    Just in case that's not greedy enough, though, you could also try:
    s/\s+([^\s])/ $1/g;

    But the second pattern won't trim trailing whitespace.
    HTH,
    --isotope
RE: 2 remaining problems (regex)
by merlyn (Sage) on Aug 25, 2000 at 02:58 UTC
    None of your examples had two keywords before a curly brace, so I didn't support that.

    Just a hint for the future: specifications help, and examples help too.

    So, another whack at this:

    my @prefix; while (<>) { next if /^#/; # skip comments, apparently if (/^\s*(.*?)\s*\{\s*$/) { push @prefix, $1; } elsif (/^\s*\}\s*$/) { pop @prefix; # no error checking } elsif (/^\s*(.*)/) { print "@prefix " if @prefix; print $1, "\n"; } else { die "internal error on $_"; } }

    But the real question I gotta ask is, "did you learn anything, or did I just do your homework for you?"

    -- Randal L. Schwartz, Perl hacker

Re: 2 remaining problems (regex)
by ChefLimo (Initiate) on Aug 25, 2000 at 03:32 UTC
    Actually, I am a network engineer for a very large isp; my boss charged me with converting config files from routers out on our network, to a flat file, and he is not the patient type. Not to kiss up, but I found "Learning Perl" very informative and challenging, as I have no prior programming experience. However, I should add to future posts, that I am perfectly willing (preferable?) to be pointed in the right direction vs. handed the answers. At any rate, thanks a bunch!