Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Regular Expression - print out certain sections of line based on the content

by jas999 (Initiate)
on Jan 02, 2009 at 14:36 UTC ( [id://733761]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, Im looking for a way to print out certain sections of line based on the content. For example, I want to print out the ENV (just whats in the brackets) if the STATUS is Not Running. Also, I want to print out whats in FIRST and in PHONE.
var1 = "ENV(ABCD1234) STATUS(Running)"
var2 = "ENV(IJK1234) STATUS(Not Running)"
var3 = "PRINT NAME FIRST(ABCD) SECOND(EFGH) ADDRESS('') PHONE(12345678)"
var4 = "PRINT NAME FIRST() SECOND(WXYD) ADDRESS('') PHONE(87654321)"

What's the best way to do this? I have tried splitting and substituting but am not succeeding. Do I need to have a specific regular expression? Any ideas?
  • Comment on Regular Expression - print out certain sections of line based on the content

Replies are listed 'Best First'.
Re: Regular Expression - print out certain sections of line based on the content
by Bloodnok (Vicar) on Jan 02, 2009 at 14:48 UTC
    It would be far more use to you (and indeed, us) if you were to provide ...
    • More detail of the context e.g. CLI/file parsing etc.
    • examples of your attempts thus far

    Having said that, this may do something like what you're after (untested code alert)..

    my @strings = ( "ENV(ABCD1234) STATUS(Running)", "ENV(IJK1234) STATUS(Not Running)", "PRINT NAME FIRST(ABCD) SECOND(EFGH) ADDRESS('') PHONE(12345678)", "PRINT NAME FIRST() SECOND(WXYD) ADDRESS('') PHONE(87654321)" ); foreach (@strings) { print $1 if /ENV\(([^)]*)\) STATUS\(Not Running\)/; print $1,$2 if /^PRINT NAME FIRST\(([^)]*)\).* PHONE\([^)]*\)/; }

    Update:

    Modded RE (to remove space after 'FIRST' - thanx to ww.

    A user level that continues to overstate my experience :-))

      "remove space after "FIRST'" ++

      But the output from your code above is:

      IJK1234 Use of uninitialized value in print at F:\_wo\pl_test\733761b2.pl line + 16. ABCD Use of uninitialized value in print at F:\_wo\pl_test\733761b2.pl line + 16.

      ...which doesn't capture the phone number.

      So, perhaps one might wish to capture the phone in the manner below and avoid the uninitialized warnings re the case where "FIRST" is empty:

      foreach my $item(@strings) { print "in ENV Not Running: " . $1 if ($item =~ /ENV\(([^)]*)\) STATU +S\(Not Running\)/); $item =~ /^PRINT NAME FIRST\(([^)]*)\).* PHONE\((.+)\)/; if ($1 && $2) { print "in PRINT NAME FIRST: $1, " . $2; } }

      OUTPUT:

      in ENV Not Running: IJK1234 in PRINT NAME FIRST: ABCD, 12345678

      Note that adding the labels to the output may bollix further processing, depending on where OP is going with this.

      Thank you for this. My reg expression was incorrect
Re: Regular Expression - print out certain sections of line based on the content
by MidLifeXis (Monsignor) on Jan 02, 2009 at 14:45 UTC

    Can you show us the code you have tried and actual output you get? Try making a small chunk of code the reproduces your problem. Otherwise, the best we can suggest (other than writing it for you - I am available for consulting, BTW) is to look at line 42.

    --MidLifeXis

      Slightly modified and tested
      @strings = ( "ENV(ABCD1234) STATUS(Running)", "ENV(IJK1234) STATUS(Not Running)", "PRINT NAME FIRST(ABCD) SECOND(EFGH) ADDRESS('') PHONE(12345678)", "PRINT NAME FIRST() SECOND(WXYD) ADDRESS('') PHONE(87654321)" ); foreach $line (@strings){ if ($line=~/ENV\((.*?)\) STATUS\(Not Running\)/){ print "$1\n"; } if ($line=~/PRINT NAME FIRST\((.*?)\) SECOND\((.*?)\) ADDRESS\ +((.*?)\) PHONE\((.*?)\)/){ print "$1 $4\n"; } }
      (: Life is short enjoy it :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://733761]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-19 04:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found