Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Multi Line Matching- getting into csv

by Tux (Canon)
on Aug 02, 2013 at 06:22 UTC ( [id://1047549]=note: print w/replies, xml ) Need Help??


in reply to Re: Multi Line Matching- getting into csv
in thread Multi Line Matching- getting into csv

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-19 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found