in reply to Multiple line records from a command string

How's this?

#! perl -slw use strict; $/ = "\nS"; m[ Device \s Symmetrix \s Name \s+ : \s+ ( [^\n]+ ) .+? Device \s Serial \s ID \s+ : \s+ ( \d+ ) .+? KiloBytes \s+ : \s+ ( \d+ ) .+? SCSI-3 \s Persistent \s Reserve : \s+ ( \S+ ) .+? ]smx and print "name:$1\nID:$2\ncap:$3\nRes:$4" while <DATA>; __DATA__ ******** Two copies of the supplied example ************

Output:

c:\test>junk75 name:0062 (VCM) ID:6000062081 cap:5760 Res:Disabled name:0062 (VCM) ID:6000062081 cap:5760 Res:Disabled

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

Replies are listed 'Best First'.
Re^2: Multiple line records from a command string
by molecules (Monk) on Feb 23, 2010 at 17:33 UTC

    (for Perceptor and others' sake)

    In BrowserUK's code:

    m[ Device \s Symmetrix \s Name \s+ : \s+ ( [^\n]+ ) .+? Device \s Serial \s ID \s+ : \s+ ( \d+ ) .+? KiloBytes \s+ : \s+ ( \d+ ) .+? SCSI-3 \s Persistent \s Reserve : \s+ ( \S+ ) .+? ]smx . . .

    The regex flags smx mean the following:

    s makes . match anything, including newlines. (In Perl, by default, . does not match newlines.)

    m causes the characters ^ and $ to match, respectively, at the beginning and ending of lines instead of the beginning and ending of strings

    x is for "Extended Formatting" or "Whitespace is not significant." Among other things, this allows the expression to span several lines without matching literal white space.

    Using these three flags in Perl regexes is a best practice described by Damian Conway in Perl Best Practices pages 236-241.
Re^2: Multiple line records from a command string
by spq (Friar) on Feb 23, 2010 at 15:40 UTC

    You could also break this up a bit and handle the input per record the way you would like. Personally I also find this a little easier to read and maintain. The key from above is setting the input line separator:

    $/ = "\nS"; # or $/ = "Symmetrix ID:\n"

    Which will allow you to open the command output to a filehandle and use a while loop, where each iteration is an individual record. You can then easily parse out the information you want:

    open(FH,"-|", "symdev list -v") or die $!; while (<FH>) { my $symm_id = my $others = undef; # or ''; if ( m/Symmetrix ID/ ) { $symm_id = ( m/Symmetrix ID\s+:\s+(\d+)/ ); } # etc }

    HTH!

      You're obviously welcome to your view, but I cannot see the purpose in making it so complicated (20+ lines instead of 7).

      Or so needlessly inefficient. Calling the regex engine 8 times (twice each for every thing you want to match), rather than once.

      And there is absolutely no reason to initialise my vars to undef.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: Multiple line records from a command string
by shmem (Chancellor) on Feb 23, 2010 at 18:18 UTC
    How's this?

    ++ Brilliant. Perfect example for translating sample input into short, concise, readable code.

      I think BrowserUk is aware of that :D
Re^2: Multiple line records from a command string
by Preceptor (Deacon) on Feb 24, 2010 at 08:39 UTC
    That's lovely, and almost exactly what I was looking for. And way less ugly than what I was trying to do with 'line based' setting of variables.