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

Re: Multi Line Matching- getting into csv

by hdb (Monsignor)
on Aug 02, 2013 at 11:59 UTC ( #1047583=note: print w/replies, xml ) Need Help??

in reply to Multi Line Matching- getting into csv

Reading this kind of data is best done IMHO by first reading all of the data into an array of hashes (one hash per user) and then print in the desired format. This way you get nice and clean code with one line for each line of your data:

use strict; use warnings; my @data; while(<DATA>){ push @data, { 'user' => $1 } if /^user: (\w+)/; $data[-1]{'summary'} = $1 if /summary\s+"(.+)"/; $data[-1]{'accesslevel'} = $1 if /access-level\s+(\S+)/; $data[-1]{'group'} = $1 if /group\s+(\S+)/; } for my $user (@data){ print join ',', map { $user->{$_} // '' } qw(user summary acce +sslevel group); print "\n"; } __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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1047583]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2018-05-25 10:09 GMT
Find Nodes?
    Voting Booth?