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

Hi monks This is what i have tried
#usr/bin/perl use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new("vrrp.xls"); my $worksheet = $workbook->add_worksheet(); $worksheet->write ("A1", 'Device'); $worksheet->write ("B1", 'Interface Name'); $worksheet->write ("C1", 'VRID'); $worksheet->write ("D1", 'State'); $worksheet->write ("E1", 'Primary ip address'); $worksheet->write ("F1", 'VRRP Address'); my $row = 2; my $line = ''; my $device; open inFile, '<', 'svrrp.txt' or die "Couldn't open svrrp.txt: $!"; while (! eof inFile) { $line .= ' ' . <inFile>; chomp $line; next if ! ($line =~ /logging\sto\sDevice\s(.*?)\s+/i) and ! eo +f inFile; my $nextDevice = $1; next if ! ($line =~ /Interface\s(.*?)\s+/i) ; my $nextInterface = $1; #next if ! ($line =~ /Interface\s(.*?)\s+/i) ; #my $nextInterface = $1; { if ($line=~ m[ (?=.*? nterface \s+ (\S+) ) (?=.*? VRID \s+ (\ +S+) ) (?=.*? State: \s+ (\S+ +) ) (?=.*? Primary\saddress: \s+ (\S+) +)]smx) { $worksheet->write("A$row", "$device"); $worksheet->write("B$row", "$1"); $worksheet->write("C$row", "$2"); $worksheet->write("D$row", "$3"); $worksheet->write("E$row", "$4"); my (@ip)=($line=~m/number addresses(?:.*?) +((?:\d{1,3}\.){3}\d{1,3})/gsi); print "The addresses are: ". join("\n",@ip +)."\n\n"; print " end\n\n"; $worksheet->write("F$row", join("\n",@ip) +); ++$row; } else { print "no match found\n" + ; } print "$device,$Interface,$1, $2, $3, $4 \n"; } $Interface = $nextInterface; ++$row; $device = $nextDevice; $line = " "; } close inFile; $workbook->close();
The logs in the file is as follows :
svrrp.txt logging to Device F01 is 19.9.16.11... Showing the vrrp status VRRP Interfaces Interface WEB_A Authentication: NoAuthentication VRID 39 State: Master Time since transition: 582693 hfghfghfghghghfghfhhfgh Primary address: 195.51.205.220 Number addresses: 1 195.51.205.222 Monitored circuits: Afgh_A (priority 1fgh50) SRghf1 (priority 1fgh50) Interface DATA_DL Nufghfghmber of vhfghl roufgh Fgfhfghs: AuthefghfgntichoAhfhication VRID 2556 State: Master Time since transition: 13405375 Bahfgh Flafghfghgs: Advertfghdl: 1 Rfghfghfgval: 3fghfghfgh fghfsgh VfghfghVRghfg Vh Primary address: 19.18.27.65 Next advertisement: 0 Number addresses: 1 15.12.57.234 I logging to Device L2 is 14.359.1.2... Showing the vrrp status VRRP Interfaces Interface SDC_A Nughujgj AuthentijghjghjNoAuthentication VRID 9 State: Backup Time since transition: 582695 Mghjghjer: hgjghjghj0 BahhjFlags: Adverghjad Interval: 3 VMAChj VMAhj1:ef Primary address: 195.51.205.221 Masghjghjg Number addresses: 1 195.51.205.222 Interface SA Numbehgjghjr of virtualj: 1 Fghjg Authentijghjtion VRID 7 State: Backup Time since transition: 582695 Master: 62.187.224.12 Baseghjghj4 Flags: Advertisement ihjghjRouterhjghj3jghjVRRP VMAC: gh +j:01jghj Primary address: 62.187.224.13 Mastghjghj Number addresses: 1 6.17.26.14
For each device there are many interfaces- and for each interface we have a VRID,STATE,PRIMARY ADDRESS and the ADDRESS below the "number address". so we need to print in the excel sheet
Device1-interface1-vrid,state... interface2-vrid,state... Device2-interface1-vrid,state...

Edited by planetscape - added readmore tags

2006-02-16 Retitled by planetscape, as per Monastery guidelines
Original title: 'what could be the problem ?'

Replies are listed 'Best First'.
Re: Trying to use Spreadsheet::WriteExcel. What could be the problem?
by spiritway (Vicar) on Feb 15, 2006 at 05:41 UTC

    On Line 17 you've got $line set up to append. If your test fails (the desired string is not found in Lines 20-21, then your $line variable keeps the stuff it already had, and appends the next line of text in your input file. I don't know what your input file looks like, but I suspect that line 17 might be the problem.

Re: Trying to use Spreadsheet::WriteExcel. What could be the problem?
by CountOrlok (Friar) on Feb 15, 2006 at 05:52 UTC
    If you analyze your input file better, you may be able to come up with a far elegant solution.

    Example, ask yourself, is an Interface information always in a block that has a form that always starts with the /^Interface\s(\w+)$/ ? If so, then read in the file, pushing each (chomped) line into an array. When you get to the next Interface block, process the array you have built up in a function (to make the code look cleaner). In this function you can iterate over each element of the array looking for the data you want to extract (vrid, state, etc) and print it out (or pass that info on to another function that does the printing).

    This way you keep your code looking clean and polished, thus easier for you and others to read and understand. It will also be easier for you to debug. You can then easily figure out why your code mixes up the interfaces and the addresses.

    -imran

      Hi

      yes

      My interface always have a form that Interface name1,Interface name2.....Interface name10

      can you figure out where i did mistake ?
      Hi yes. Interface is always have form Interface name1, Interface name2 ....Interface name10 cann you figure it out where i did mistake ?
        Here is a sample of what a cleaner (in my opinion) version of your code should look like:
        #... my @lines = (); open my $inFile, '<', 'svrrp.txt' or die "Couldn't open svrrp.txt: $!" +; my $currIface = ""; while (<$inFile>) { chomp; if (my $iface = ($_ =~ /^Interface\s(\w+)$/) { processlines($currIface, @lines); $currIface = $iface; @lines = (); } push @lines, $_; } processlines($currIface, $lines); # to process last interface block #... sub processlines() { my $iface = shift; return unless $iface; # declare any vars foreach (@_) { # do your pattern matching here } # print your findings }
        You can expand on this to make it suit you.

        -imran

Re: Trying to use Spreadsheet::WriteExcel. What could be the problem?
by izut (Chaplain) on Feb 15, 2006 at 10:34 UTC
    I suggest you to use
    next if ($line !~ /logging\sto\sDevice\s(.*?)\s+/i) and !eof(inFile);
    instead of
    next if ! ($line =~ /logging\sto\sDevice\s(.*?)\s+/i) and ! eof inFile;

    Use parentheses wherever you can, it will help us to read your code :) As perlstyle says, the fact that you can omit parentheses doesn't means that you should not write them :).

    Igor 'izut' Sutton
    your code, your rules.

      Hi Nope that need not be.... Looping inbetween the interfaces is not happening ?
Re: Trying to use Spreadsheet::WriteExcel. What could be the problem?
by Anonymous Monk on Feb 15, 2006 at 03:30 UTC
    What is symptom?
      Hi

      Thank you
      Looping is not happening within the each interface that
      is the problem.