Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Simple parse of text file to csv

by Tux (Monsignor)
on Dec 19, 2012 at 07:19 UTC ( #1009516=note: print w/ replies, xml ) Need Help??


in reply to Re: Simple parse of text file to csv
in thread Simple parse of text file to csv

$1 in the push should have been $key. Translated to Text::CSV_XS

use strict; use warnings; use Text::CSV_XS; my %values; my $key; my @keys; while (<DATA>) { chomp; m/^\s*#/ and next; # Skip comment m/^\s*$/ and next; # Skip empty lines if (m/^\s* (.+?) \s*:\s* $/x) { $key = $1; push @keys, $key; next; } s/^\s+//; s/\s+$//; push @{ $values{$key} }, $_; } my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1, eol => "\n +" }); $csv->print (*STDOUT, \@keys); $csv->print (*STDOUT, [ map { "@$_" } @values{@keys} ]);

With the __DATA__ section added, results in:

Host,Kernel,Version,"Hot fixes","Enabled Features" NetDevice1234,"Linux 2.6.18-164.11","Driver 23.4.1 333.0 Hotfix BLD Ed +ition","HF372590 HF372804 HF372864","FTP HTTP SMTP Active Directory"


Enjoy, Have FUN! H.Merijn


Comment on Re^2: Simple parse of text file to csv
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2015-07-06 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls