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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.