You are heading for disaster by stripping the "'s from "summary". If there are ,'s in the summary, your CSV will be corrupt. Another point is that not all records seem to have the same fields defined. One could do better by being more generic. And finally, use a decent CSV module for output like Text::CSV_XS or Text::CSV.

#!/usr/bin/perl use 5.014; use warnings; use Text::CSV_XS; my %hdr; my @dta; my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1, eol => "\r +\n" }); { local $/ = ""; while (<DATA>) { my %data = m/^\s*(\w[^ :]+)[: ]+(.*\S)\s*$/gm; $hdr{$_}++ for keys %data; push @dta, \%data; } my @hdr = keys %hdr; $csv->print (*STDOUT, \@hdr); $csv->print (*STDOUT, [ @{$_}{@hdr} ]) for @dta; } __END__ user: myus44 [up] ------------ admin-state enabled summary "Johnny Cash" access-level group-defined group mi-group [up] user: jar1543 [up] ------------ admin-state enabled summary "Lara Croft" access-level group-defined group jar-head [up] user: myprivilegeduser [up] ----------- admin-state enabled access-level privileged

will produce:

group,summary,user,access-level,admin-state "mi-group [up]","""Johnny Cash""","myus44 [up]",group-defined,enabled "jar-head [up]","""Lara Croft""","jar1543 [up]",group-defined,enabled ,,"myprivilegeduser [up]",privileged,enabled

This way the data is exactly what it was. If you do not want the double quotes, but still be safe:

while (<DATA>) { my %data = m/^\s*(\w[^ :]+)[: ]+(.*\S)\s*$/gm; $hdr{$_}++ for keys %data; s/^"(.*)"$/$1/ for values %data; push @dta, \%data; }

To produce

group,summary,user,access-level,admin-state "mi-group [up]","Johnny Cash","myus44 [up]",group-defined,enabled "jar-head [up]","Lara Croft","jar1543 [up]",group-defined,enabled ,,"myprivilegeduser [up]",privileged,enabled

And finally, if you want the user as first/key field, proceed like

delete $hdr{user}; my @hdr = ("user", keys %hdr);

to get

user,group,summary,access-level,admin-state "myus44 [up]","mi-group [up]","Johnny Cash",group-defined,enabled "jar1543 [up]","jar-head [up]","Lara Croft",group-defined,enabled "myprivilegeduser [up]",,,privileged,enabled

Side note: Text::CSV_XS will have no problems with undefined fields


Enjoy, Have FUN! H.Merijn

In reply to Re^2: Multi Line Matching- getting into csv by Tux
in thread Multi Line Matching- getting into csv by symgryph

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.