• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

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

0
richsark
Asked:
richsark
3 Solutions
 
Carl BohmanCommented:
#!/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 KnightIT 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Thanks very much
0
 
parparovCommented:
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now