parse an input file and create new output file

HI,

I require your expertise please.

I have a log file thats structured like so: (( See Code snippet ))

I am looking for a way to parse this file named "show-zones.log" and look for any "delegate_to" that has 255.255.255.255 with the matching "display_domain"

if it does, I need to create a new txt file named "delete-zones.ibcli" that shows it like so:

conf zone delete 84.117.142.in-addr.arpa
conf zone delete 85.117.142.in-addr.arpa
conf zone delete 86.117.142.in-addr.arpa


Thank you in advanced !


--- ---
                   delegate_to : dns-slave.as.qc.foo.ca,255.255.255.255
                   delegate_to : dns-master.as.qc.foo.ca,255.255.255.255
                display_domain : 84.117.142.in-addr.arpa
             ms_allow_transfer : NSOnly
                  ms_ddns_mode : None
                    ms_managed : None
                          name : 142.117.84.0/24
                        prefix : 84
                         views : default
--- ---
                   delegate_to : dns-slave.as.qc.foo.ca,255.255.255.255
                   delegate_to : dns-master.as.qc.foo.ca,255.255.255.255
                display_domain : 85.117.142.in-addr.arpa
             ms_allow_transfer : NSOnly
                  ms_ddns_mode : None
                    ms_managed : None
                          name : 142.117.85.0/24
                        prefix : 85
                         views : default
--- ---
                   delegate_to : dns-slave.as.qc.foo.ca,255.255.255.255
                   delegate_to : dns-master.as.qc.foo.ca,255.255.255.255
                display_domain : 86.117.142.in-addr.arpa
             ms_allow_transfer : NSOnly
                  ms_ddns_mode : None
                    ms_managed : None
                          name : 142.117.86.0/24
                        prefix : 86
                         views : default
--- ---

Open in new window

LVL 1
richsarkAsked:
Who is Participating?
 
Carl BohmanConnect With a Mentor Commented:
#!/usr/bin/perl
use strict;
use warnings;

my $Delete = 0;

while (<DATA>)
{
        if (/^--- ---/)
        {
                $Delete = 0;
        } elsif (/^\s*delegate_to\s*:/)
        {
                $Delete = (/,255\.255\.255\.255\s*$/);
        } elsif (/^\s*display_domain\s*:\s*(\S+)/)
        {
                print "conf zone delete $1\n" if ($Delete);
        }
}

__DATA__
--- ---
                   delegate_to : dns-slave.as.qc.foo.ca,255.255.255.255
                   delegate_to : dns-master.as.qc.foo.ca,255.255.255.255
                display_domain : 84.117.142.in-addr.arpa
             ms_allow_transfer : NSOnly
                  ms_ddns_mode : None
                    ms_managed : None
                          name : 142.117.84.0/24
                        prefix : 84
                         views : default
--- ---
                   delegate_to : dns-slave.as.qc.foo.ca,255.255.255.255
                   delegate_to : dns-master.as.qc.foo.ca,255.255.255.255
                display_domain : 85.117.142.in-addr.arpa
             ms_allow_transfer : NSOnly
                  ms_ddns_mode : None
                    ms_managed : None
                          name : 142.117.85.0/24
                        prefix : 85
                         views : default
--- ---
                   delegate_to : dns-slave.as.qc.foo.ca,255.255.255.255
                   delegate_to : dns-master.as.qc.foo.ca,255.255.255.255
                display_domain : 86.117.142.in-addr.arpa
             ms_allow_transfer : NSOnly
                  ms_ddns_mode : None
                    ms_managed : None
                          name : 142.117.86.0/24
                        prefix : 86
                         views : default
--- ---

Open in new window

0
 
Steve KnightConnect With a Mentor IT ConsultancyCommented:
The code below saved as "parse.cmd" or whatever works for me on your sample.

Steve

C:\4. WIP\EE\27387976>parse
conf zone delete 84.117.142.in-addr.arpa
conf zone delete 85.117.142.in-addr.arpa
conf zone delete 86.117.142.in-addr.arpa

@echo off
setlocal enabledelayedexpansion

set input=input.txt
set output=output.txt

set delegate=0
for /f "tokens=1,2,3 delims=:," %%A in ('findstr /I /L "delegate_to display_domain" input.txt') do (
  set field=%%A
  set field=!field: =!
 
  if /i "!field!" EQU "delegate_to" if "%%C" EQU "255.255.255.255" set delegate=1
  if "!delegate!" EQU "1" if "!field!" EQU "display_domain" (
    echo conf zone delete%%B
    echo conf zone delete%%B >> %output%
    set delegate=0
   )
)

Open in new window

0
 
Steve KnightIT ConsultancyCommented:
Hmm, why did this pop up in Windows batch scripting before?!
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
richsarkAuthor Commented:
Thanks guys, let me try and revert .

Thanks very much
0
 
parparovConnect With a Mentor Commented:
bounsy's code will fail if there is a second delegate_to which is not 255.255.255.255, however it is unclear if such scenario is possible, or whether this behaviour is unwanted.

To force it to work even when second delegate_to doesn't display 255.255.255.255 line 14 may be changed to:
                $Delete ||= (/,255\.255\.255\.255\s*$/);

Open in new window

0
 
richsarkAuthor Commented:
Hi, yes... Correct , there might not be a second delegate. Good catch parparov.

Thanks you kindly
0
 
richsarkAuthor Commented:
Hi guys. I have used bouncy script with the modification of parparov .

Though I thank dragon-it, for a windows script. I did not use that one.

Thanks all for your help
0
 
Steve KnightIT ConsultancyCommented:
No problem, didn't see perl, just popped up in my alerts for Windows scripting.  Still don't know how!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.