That small typo 'fileds' (fields) obscured the meaning of your question for me, and rereading it I think that what you wanted is really more like this:

(again, I've implemented against postgres, but if you take out the DB stuff you can use CSV output (the outcommented lines))

use strict; use warnings; use DBI; my $t = "pm1083865_ivo2"; # table name my $date_string ; my @hdr = (); my @val = (); while (<>) { # reading STDIN chomp; $date_string = $_; last; } while (<>) { # reading STDIN next if (/^\s*$/); chomp; if ( /^([^:]+):\s*(\d+)\s*$/ ) { push(@hdr, $1); push(@val, $2); } elsif ( /^([^:]+):\s*(\d+)\s*([^:]+):\s*(\d+)\s*$/ ) { push(@hdr, $1); push(@val, $2); push(@hdr, $3); push(@val, $4); } else { print ">>>", $_ ,"<<<\n"; die "oops - unprocessed line!\n"; } } # clean up header names for database use: for (my$i=0;$i<scalar(@hdr);$i++) { $hdr[$i] = lc($hdr[$i]); # lower case $hdr[$i] =~ s{[-]}{_}g; # change dash to underscore $hdr[$i] =~ s{[ ]}{_}g; # change space to underscore $hdr[$i] =~ s{_[_]+}{_}g; # } my @all_hdrs = (); push(@all_hdrs, "date_line" ); push(@all_hdrs, @hdr +); my @all_vals = (); push(@all_vals, $date_string); push(@all_vals, @val +); # use only this line to get a output csv # print join("\t", @all_hdrs), "\n", join("\t", @all_vals), "\n"; # exit; my $dbh = DBI->connect or die "no database connection - $!\n "; $dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; my $arf = $dbh->selectrow_arrayref(" select count(*) from information_schema.tables where table_schema = 'public' and table_name = '$t'"); if ($arf->[0] == 0) { my $sql_create = join("\n" , "create table $t (" , " date_line timestamptz\n, " , join ("\n, ", map { $_ .= ' integer' } @hdr ) , ");" ); $dbh->do( $sql_create ); } my $sql_insert = join("\n" , "insert into $t values (" , join( ", ", map {'?'} @all_hdrs) , ");" ); my $sth = $dbh->prepare($sql_insert); my $rc = $sth->execute(@all_vals); $dbh->commit; $dbh->disconnect;

After running that (showing record vertically):

$ echo "table pm1083865_ivo2" | psql -x | head -n 20 Timing is on. -[ RECORD 1 ]-----------------+----------------------- date_line | 2014-04-25 23:06:25+02 total_subscribers | 970260 active | 86806 dormant | 883454 pdsn_simple_ipv4 | 212 pdsn_simple_ipv6 | 0 pdsn_mobile_ip | 539792 ha_mobile_ipv6 | 0 hsgw_ipv6 | 234943 hsgw_ipv4 | 1977 hsgw_ipv4_ipv6 | 192328 pgw_pmip_ipv6 | 0 pgw_pmip_ipv4 | 0 pgw_pmip_ipv4_ipv6 | 0 pgw_gtp_ipv6 | 0 pgw_gtp_ipv4 | 0 pgw_gtp_ipv4_ipv6 | 0 sgw_gtp_ipv6 | 0 [...] etc., there are 126 columns

In reply to Re^4: Parsing cli output into .csv by erix
in thread Parsing cli output into .csv by Ivo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.