xossa has asked for the wisdom of the Perl Monks concerning the following question:

Hi, This is an extract of my file : Total Hard Errors: 0 Total Soft Errors: 0 Total Queue Length: 0 Name LUN 259 Minimum latency reads N/A How can I get the number '259' my script : open L_NAVICLI , "$NAVICLI $IP1 getall |" ; while (<L_NAVICLI>) { if (/Name.*LUN.*.(\d+)/){ $lunid = $3; print "\n!$lunid!\n"; print $_; exit; } } close L_NAVICLI; When i execute it, it returns : # perl capacx.pl !! Name LUN 259 Can you help me please to get the number '259' in my var $lunid ?? Thanks a lot Xossa

Replies are listed 'Best First'.
Re: Parse a File and get a value
by toolic (Bishop) on Nov 09, 2011 at 18:38 UTC
    Two mistakes:
    • Use $1 instead of $3 to get the 1st capture (you only have one set of parens).
    • Use \s+ instead of the greedy .*
    use warnings; use strict; while (<DATA>) { if (/Name\s+LUN\s+(\d+)/){ my $lunid = $1; print "\n!$lunid!\n"; print $_; exit; } } __DATA__ Total Hard Errors: 0 Total Soft Errors: 0 Total Queue Length: 0 Name LUN 259 Minimum latency reads N/A
    prints:
    !259! Name LUN 259
    Aside: you only need to use code tags for your code and data, not for your question: Writeup Formatting Tips