load  CSV file and convert contents

Posted on 2011-04-24
Last Modified: 2012-05-11
Hi, I reference to my post:

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

When I run it as: ipnets.csv

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

But when I run as: 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.



use strict;
use warnings;

my %masks = (
    8 => '',
    15 => '',
    16 => '',
    20 => '',
    21 => '',
    22 => '',
    23 => '',
    24 => '',
    25 => '',
    26 => '',
    27 => '',
    28 => '',
    29 => '',
    30 => '',
    32 => '',

#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 (<>) {
    next if m{^Network,Gateway}; # skip header row
    unless (m{^([\d\.]+)/(\d+),([\d\.]*),(.*)$}) {
        warn "could not parse record:\n$_\n";
    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

Question by:richsark
    LVL 1

    Author Comment

    Here is the source contents

    LVL 31

    Expert Comment

    Did you try < ipnets.csv > rich.scv
    LVL 25

    Accepted Solution

    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


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

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
    Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now