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

LVL 1
richsarkAsked:
Who is Participating?
 
clockwatcherConnect With a Mentor Commented:
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
 
richsarkAuthor Commented:
Here is the source contents

Thanks
ipnets.csv
0
 
farzanjCommented:
Did you try

CSV-convert.pl < ipnets.csv > rich.scv
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
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.