Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^4: Processing CSV File

by Kenosis (Priest)
on Oct 04, 2012 at 18:09 UTC ( #997281=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Processing CSV File
in thread Processing CSV File

Hi, Perl3r.

Try the following:

#!/usr/bin/perl # use strict; use warnings; $ARGV[0] or die "Usage: $0 <filename> [<filename>] ..."; my ( $template_file_name, $templateText, %hash ) = ''; my @vars = split "\n", <<END; ##location## ##rtrname## ##loop0-ip## ##frame-ip## ##frame-DLCI## ##eth0-ip## END while (<>) { chomp; my @fields = split /,/; my $templateFN = pop @fields; if ( $template_file_name ne $templateFN ) { $template_file_name = $templateFN; undef $templateText; } @hash{@vars} = @fields; $templateText //= getTemplateText($template_file_name); my $templateTextCopy = $templateText; $templateTextCopy =~ s/$_/$hash{$_}/g for keys %hash; my $ofile_name = $hash{'##rtrname##'} . '.txt'; open my $fh, '>', $ofile_name or die "$ofile_name: $!"; print $fh $templateTextCopy; close $fh; } sub getTemplateText { my ($template_file_name) = @_; local $/; open my $fh, '<', $template_file_name or die "$template_file_name: + $!"; $templateText = <$fh>; close $fh; return $templateText; }

The last CSV column needs to contain the template's file name. The script can handle multiple files at once, e.g.:

processCSV.pl csv1.csv csv2.csv

The script uses Perl's 'defined-or-equals' operator to only read the template's text once, and will read in a new template if it detects a change in the template's file name (e.g., when going from csv1.csv to csv2.csv).


Comment on Re^4: Processing CSV File
Select or Download Code
Re^5: Processing CSV File
by Anonymous Monk on Oct 05, 2012 at 00:44 UTC

    Thanks Kenosis!! Your help is invaluable.. Ok my .pl as it stands is as follows;

    #!/usr/bin/perl # use strict; use warnings; $ARGV[0] or die "Usage: $0 <filename> [<filename>] ..."; my ( $template_file_name, $templateText, %hash ) = ''; my @vars = split "\n", <<END; ##location## ##rtrname## ##loop0-ip## ##ospf-id## ##ospf-area## ##ospf-network## ##ospf-mask## ##eth00-ip## ##eth00-sm## END while (<>) { chomp; my @fields = split /,/; my $templateFN = pop @fields; if ( $template_file_name ne $templateFN ) { $template_file_name = $templateFN; undef $templateText; } @hash{@vars} = @fields; $templateText //= getTemplateText($template_file_name); my $templateTextCopy = $templateText; $templateTextCopy =~ s/$_/$hash{$_}/g for keys %hash; my $ofile_name = './Config_Output/' . $hash{'##rtrname##'} . '.txt +'; open my $fh, '>', $ofile_name or die "$ofile_name: $!"; print $fh $templateTextCopy; close $fh; } sub getTemplateText { my ($template_file_name) = @_; local $/; open my $fh, '<', './Templates/' . $template_file_name . '.txt' or + die "$template_file_name: $!"; $templateText = <$fh>; close $fh; return $templateText; }

    I still have a couple of problems, I was hoping you may be able to help with..
    1. Is there a way that I can ignore the first line of the CSV? - In the CSV the first line is just column descriptions, and is not needed for the output..
    2. I can still not seem to strip the newline off the end of the CSV.. I have tried to replace chomp

    with

    s/\r\n//

    but that doesn't seem to do it.. unfortunately the csv needs to be edited with MS Excel, so when saving that, it adds its extra MS stuff to the file
    3. Even better - but i don't know if this is even possible.. would there be a way, so that rather than specifying all the template variables (i.e. ##eth00-ip##, ##location## etc), I could have them all listed on the second line of the CSV, and then the code can draw those values out...?

    I really appreciate your assistance, and will definitely have to buy you a beer!!

      Am glad it's working for you.

      The script below addresses #1 and #3. Am unclear about #2. Where is that newline appearing? Here's the script:

      #!/usr/bin/perl # use strict; use warnings; $ARGV[0] or die "Usage: $0 <filename> [<filename>] ..."; my ( $template_file_name, $templateText, %hash ) = ''; for my $csvFile (@ARGV) { print "\nProcesing file $csvFile ...\n"; open my $csvfh, '<', $csvFile or die "Unable to open $csvFile: $!" +; # Ignore column names in first line my $columnNames = <$csvfh>; # Get vars from second line chomp( my $vars = <$csvfh> ); my @vars = split /,/, $vars; while (<$csvfh>) { chomp; my @fields = split /,/; my $templateFN = pop @fields; if ( $template_file_name ne $templateFN ) { $template_file_name = $templateFN; undef $templateText; } @hash{@vars} = @fields; $templateText //= getTemplateText($template_file_name); my $templateTextCopy = $templateText; $templateTextCopy =~ s/$_/$hash{$_}/g for keys %hash; my $ofile_name = $hash{'##rtrname##'} . '.txt'; print "Writing to file: $ofile_name\n"; open my $fh, '>', $ofile_name or die "$ofile_name: $!"; print $fh $templateTextCopy; close $fh; } close $csvfh; } print "\nDone!\n"; sub getTemplateText { my ($template_file_name) = @_; local $/; open my $fh, '<', $template_file_name or die "$template_file_name: + $!"; $templateText = <$fh>; close $fh; return $templateText; }

        You deserve a medal! That works perfectly!
        The problem with #2 is;
        If i open the .CSV in MS Excel, make a change and save it, then when i run the .pl, i get the following error:

        Use of uninitialized value $vars in chomp at create-configs.pl line 18 +, <$csvfh> line 1. Use of uninitialized value $vars in split at create-configs.pl line 19 +, <$csvfh> line 1.

        However, if i open the .CSV in excel, make the changes, then open it with something like Text Edit, and go to the end of each line in the CSV, and press delete, then enter (to put evreything back to its own line), then when I run the .pl it works perfectly..
        Does that make sense? if not, let me know, and I will try and send you the files if possible, so you can know what i mean..
        Once again, your help is invaluable..

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997281]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-07-13 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (242 votes), past polls