We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

load  CSV file and convert contents

richsark
richsark asked
on
Medium Priority
212 Views
Last Modified: 2012-05-11
Hi, I reference to my post: https://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

Comment
Watch Question

Author

Commented:
Here is the source contents

Thanks
ipnets.csv
CERTIFIED EXPERT

Commented:
Did you try

CSV-convert.pl < ipnets.csv > rich.scv
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
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.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.