Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
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 surveying the Monastery: (5)
As of 2014-09-18 01:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (104 votes), past polls