[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

load CSV file and convert contents

Hi, I reference to my post: http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_26945034.html

I tried to fix the code that Wilcoxon did, but I dont know what is wrong

When I run it as:

CSV-convert.pl ipnets.csv

It spits it out on the screen. I guess the code is correct right??

But when I run as:
CSV-convert.pl ipnets.csv > rich.scv

I get a message of
could not parse record:
Network,EA-Gateway:,Description:,VLAN:,Site:,comment,Purpose:,IGP ID:,IGP Number:,Sitecode:

What is wrong here?

I will post the input on next post.

Thanks




#!/usr/bin/perl

use strict;
use warnings;

my %masks = (
    8 => '255.0.0.0',
    15 => '255.254.0.0',
    16 => '255.255.0.0',
    20 => '255.255.240.0',
    21 => '255.255.248.0',
    22 => '255.255.252.0',
    23 => '255.255.254.0',
    24 => '255.255.255.0',
    25 => '255.255.255.128',
    26 => '255.255.255.192',
    27 => '255.255.255.224',
    28 => '255.255.255.240',
    29 => '255.255.255.248',
    30 => '255.255.255.252',
    32 => '255.255.255.255',
);

#open IN, 'ININD-IP-Data.csv' or die "could not open csv: $!";

print "HEADER-Network,Address*,Netmask*,EA-Gateway,EA-Description,EA-VLAN,Comment,EA-Site,EA-Purpose,EA-IGP ID,EA-IGP Number,EA-Sitecode\n";

while (<>) {
    chomp;
    next if m{^Network,Gateway}; # skip header row
    unless (m{^([\d\.]+)/(\d+),([\d\.]*),(.*)$}) {
        warn "could not parse record:\n$_\n";
        next;
    }
    my ($addr, $mask, $gate, $rest) = ($1, decode_mask($2), $3, $4);
    $gate = 'Not Provided' unless $gate;
    $rest =~ s{[/\$!']}{}g; # remove special characters
    while ($rest =~ m{^([^"]*)"([^"]+)"}) {
        my ($beg, $qstr) = ($1, $2);
        my $ori = "$beg\"$qstr\"";
#        $qstr =~ s{,}{ }g;
        $rest =~ s{^\Q$ori}{$beg$qstr};
    }
    $rest =~ s{\s\s+}{ }g; # remove extra spaces
    $rest =~ s{\.,}{,}g; # remove periods just before comma
    my $str = "Network,$addr,$mask,$gate,$rest";
    print $str, "\n";
}

sub decode_mask {
    my ($mask) = @_;
    return $masks{$mask} if exists($masks{$mask});
    die "don't have mask $mask mapped\n";
}

Open in new window

0
richsark
Asked:
richsark
  • 2
1 Solution
 
richsarkAuthor Commented:
Here is the source contents

Thanks
ipnets.csv
0
 
farzanjCommented:
Did you try

CSV-convert.pl < ipnets.csv > rich.scv
0
 
clockwatcherCommented:
Warnings get printed to STDERR.  You're only redirecting STDOUT to the file.  If you could scroll back far enough, you'd see that warning in what runs to the screen as well.  

The test for the header row isn't working correctly.  Try changing this:

  next if m{^Network,Gateway};  # skip header row

To:

  next if m{^Network,};  # skip header row
0
 
clockwatcherCommented:
Either that or just ignore the warning.  The contents of your rich.scv should be essentially what you saw to the screen.  If they're different let us know but (other than the warning which you just didn't see scroll by on the console) they shouldn't be.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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