Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Print only if pattern matches

by Kenosis (Priest)
on Jan 24, 2013 at 06:37 UTC ( #1015082=note: print w/ replies, xml ) Need Help??


in reply to Print only if pattern matches

Place your matching regex in an if statement. If true, you can print your captures w/o error. The else can handle the exceptions:

use strict; use warnings; while (<DATA>) { next unless /^\d+\|(\d+?)\|/ and $1 == 1; if (/^(.*\|)*.*ABC\.pi=([\d.]+|[\w.]+)*.*ABC\.id=(\d+|[\w.]+).*$/) + { print "$1$2|$3\n"; } else { print "Exception: $_"; } }

Output on your data:

123|1|456464|645646|4546|112.33.44.55.66|789137136770 123|1|456464|645646|4546|112.33.44.55.67|789134713670 123|1|456464|645646|4546|112.33.44.55.68|789137213670 123|1|456464|645646|4546|112.33.44.55.69|78913713670 Exception: 123|1|456464|645646|4546|654~abc~dhghga~12.33.44.55.70~3713 +670 123|1|456464|645646|4546|112.33.44.55.70|78913713670 123|1|456464|645646|4546|112.33.44.55.70|78913713670 123|1|456464|645646|4546|112.33.44.55.70|789137135670 123|1|456464|645646|4546|112.33.44.55.70|789137153670 Exception: 123|1|456464|645646|4546|654~abc~dhghga~12.33.44.55.70~3713 +670 Exception: 123|1|456464|645646|4546|654~abc~dhghga~121322~456466874~87 +96896


Comment on Re: Print only if pattern matches
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015082]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (15)
As of 2015-07-07 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (88 votes), past polls