in reply to Parsing Text into CSV

I think you're making the surrounding code too complicated:

while (<IN>) { next if /^\s*$/; chop; /^Username\:\s+ (.*?) \s+ Owner\:\s+ (.*?)\s*$/x and do { print "$1,$2,"; next }; /^Last Login:\s+(\d+.*?\:\d+).*,\s*(\d+.*?\:\d+)/ and print "$1,$2\n"; }
Tested to work on your sample data.
<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet

Replies are listed 'Best First'.
Re^2: Parsing Text into CSV
by bowei_99 (Friar) on Jan 19, 2006 at 19:26 UTC
    Oh, actually, I forgot - the code also has to deal with lines like
    Last Login: (none) (interactive), (none) (non-in +teractive) Last Login: (none) (interactive), 12-SEP-1997 11:33 (non-in +teractive) Last Login: 11-JUL-2002 18:08 (interactive), (none) (non-in +teractive)
    I figured I'd add to radiant.matrix's code like
    /^Last Login:\s+.*,\s*(\d+.*?\:\d+)/ and print "$1,$2\n";
    but it's giving an error 'Unrecognized escape \d passed through..' I figure adding a condition to BrowserUK's regexp would work, too...

      And what output would you want in those cases? The text '(none)' where the date should be?

      #! perl -slw use strict; while( <DATA> ) { $_ .= <DATA>; print join',', m[ Username: \s+ (\S+) \s+ Owner: \s+ ([^\n]+ ) \n Last\sLogin: \s+ ( .+ ) \s \( [^,]+, \s+ (.+) \s\( ]x; } __DATA__ Username: JANDERSON Owner: JOE ANDERSON Last Login: 14-JAN-2002 08:14 (interactive), 4-MAR-2002 17:09 (non-in +teractive) Username: PBARRETT Owner: PAUL BARRETT Last Login: 18-JAN-2006 08:14 (interactive), 24-MAR-2005 17:09 (non-i +nteractive) Username: LARRY Owner: LARRY FINE Last Login: (none) (interactive), (none) (non-in +teractive) Username: MOE Owner: HARRY MOSES HOWARD Last Login: 11-JUL-2002 18:08 (interactive), (none) (non-in +teractive) Username: CURLY Owner: JEROME HOWARD Last Login: (none) (interactive), 12-SEP-1997 11:33 (non-in +teractive)

      Gives

      P:\test>junk JANDERSON,JOE ANDERSON,14-JAN-2002 08:14,4-MAR-2002 17:09 PBARRETT,PAUL BARRETT,18-JAN-2006 08:14,24-MAR-2005 17:09 LARRY,LARRY FINE,(none),(none) MOE,HARRY MOSES HOWARD,11-JUL-2002 18:08,(none) CURLY,JEROME HOWARD,(none),12-SEP-1997 11:33

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Yup, thanks, BrowserUK - that worked great :)