Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Multi Line Matching- getting into csv

by choroba (Abbot)
on Aug 01, 2013 at 22:11 UTC ( #1047508=note: print w/ replies, xml ) Need Help??


in reply to Multi Line Matching- getting into csv

Your records seem to be delimited by an empty line. You can therefore profit from the "paragraph mode" and process the records one by one:

#!/usr/bin/perl use warnings; use strict; use feature qw(say); { local $/ = q(); while (<DATA>) { my ($user) = /user: (\S*)/; my ($summary) = /summary "(.*?)"/; my ($access) = /access-level (\S*)/; my ($group) = /group (\S*)/; say join ',', map $_ // q(), $user, $summary, $access, $group; } } __DATA__ 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
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Multi Line Matching- getting into csv
Download Code
Re^2: Multi Line Matching- getting into csv
by Tux (Monsignor) on Aug 02, 2013 at 06:22 UTC

    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
Node Status?
node history
Node Type: note [id://1047508]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-09-19 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (150 votes), past polls