Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Multi Line Matching- getting into csv

by hdb (Prior)
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2016-07-01 09:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...











    Results (409 votes). Check out past polls.