These 2 lines:

if (/^        file-id $i/) {
and
elsif (/^        exit/) {


Need to be like this:

if (/^\s+file-id $/i) { # 2 CHANGES HERE - PAY CLOSE ATTENTION
and
elsif (/^\s+exit/i) {


I am not sure this will fix all your
problems.


Mike


On 11/21/2018 5:08 AM, Amanda Paziuk wrote:
Folks,

I'm hoping someone can assist as I'm having difficulty with parsing a section of the following configuration:

This is the code I have:

    open (IN, $file); # only sharing this because you need to know where @list is derived.
    while (<IN>) {
        chomp;
        next unless /file-id/;
        my $datum = $_;
        $datum =~ s/(^\s+|\s+$)//g;
        $datum =~ s/file-id //g;
        push @list, $datum; # should only contain '1', and '3'
    }
    close IN;

    # ideally this would take a snippet of that config
    $count = 0;
    foreach my $i (@list){ # loops through dynamically-learned file IDs
        open (IN, $file);
        while (<IN>) {
            chomp;
            if (/^        file-id $i/) {
                $count = 1;
            }
            elsif (/^        exit/) { # this just keeps matching every exit with that same indent
                $count = 0;
            }
            elsif ($count) {
                if (/text/) {
                    push @logfiles, $_; # successfully captures only what I want in the array
                }
            }
        }
        close IN;
    }

The first is to find all file-id lines then isolate for the number; the second loop is to take a snip:

        file-id 1
            text
        exit

What it winds up doing is:

        file-id 1
            text
        exit
        file-id 3
            text
        exit
/        exit/
/        exit/
/        exit/
/        exit/
/        exit /
        ...

This is the nightmare config structure that I need to deal with throughout the file..lots of "exits" so it matches an awful lot...

    log
        file-id 1
            text
        exit
        file-id 3
            text
        exit
    exit

The configuration is thousands of lines long, indented like that with exit at various levels, so this is just one part of what I'm trying to analyze. What it's doing is making the server work harder with all the extra stuff it's catching and leaves me wide-open for false positives...

My question: does anyone have a better way to take those snippets, stopping after the first match of "exit"? (Please don't flame, I'm asking for help.)

Amanda

Reply via email to