I have some input like this:

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

I have written the following program which seems to regex correctly, but getting things to 'print' correctly seems to require more than the naieve 'if' logic I am applying. Was wondering if someone could take a peek and suggest something better.

#!/usr/bin/perl -w while (<>) { if (/^user:/) { chomp(); $uid=$_; $uid=~m/user:\s+(\S+)/; $uid=$1; #print "$uid,"; } if (/summary/) { chomp(); $summary=$_; $summary=~m/summary\s+\"(.+)\"/; $summary=$1; #print "$summary,"; } if (/access-level/) { chomp(); $accesslevel=$_; $accesslevel=~m/access-level\s+(\S+)/; $accesslevel=$1; #print "$accesslevel,"; } if (/group\s+/) { chomp (); $group=$_; $group=~m/group\s+(\S+)/; $group=$1; #print "$group"; } print "$uid,$summary,$accesslevel,$group\n"; }

My hoped format was something like:

myuser,Johnny Cash,group-defined,migroup myprivilegeduser,,privileged,,

With 'unused' things left as ,,'s.

"Two Wheels good, Four wheels bad."

In reply to Multi Line Matching- getting into csv by symgryph

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



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