Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Print only if pattern matches

by 2teez (Priest)
on Jan 24, 2013 at 06:52 UTC ( #1015086=note: print w/replies, xml ) Need Help??


in reply to Print only if pattern matches

Your long regex

s/^(.*\|)*.*ABC\.pi=([\d.]+|[\w.]+)*.*ABC\.id=(\d+|[\w.]+).*$/$1$2 +|$3/s;
can be futher reduced to match your required output.
Using the solution provided by kenosis like so:
use strict; use warnings; while (<DATA>) { next unless /^\d+\|(\d+?)\|/ and $1 == 1; if (/(.+?)~.+?=(.+?)~.+=(.+?)$/) { # note here print $1, $2, $3, $/; } else { print "Exception: ", $_, $/; } } __DATA__ 123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.66~ABC.id= +789137136770 123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.67~ABC.id= +789134713670 123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.68~ABC.id= +789137213670 123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.69~ABC.id= +78913713670 123|1|456464|645646|4546|654~abc~dhghga~12.33.44.55.70~3713670 123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.70~ABC.id= +78913713670 123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.70~ABC.id= +78913713670 123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.70~ABC.id= +789137135670 123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.70~ABC.id= +789137153670 123|1|456464|645646|4546|654~abc~dhghga~12.33.44.55.70~3713670 123|1|456464|645646|4546|654~abc~dhghga~121322~456466874~8796896 123|2|456464|645646|4546|654~abc~dhghga~121322~456466874~6788708 123|2|456464|645646|4546|654~abc~dhghga~121322~456466874~6806
Output:
123|1|456464|645646|4546|654112.33.44.55.66789137136770 123|1|456464|645646|4546|654112.33.44.55.67789134713670 123|1|456464|645646|4546|654112.33.44.55.68789137213670 123|1|456464|645646|4546|654112.33.44.55.6978913713670 Exception: 123|1|456464|645646|4546|654~abc~dhghga~12.33.44.55.70~3713 +670 123|1|456464|645646|4546|654112.33.44.55.7078913713670 123|1|456464|645646|4546|654112.33.44.55.7078913713670 123|1|456464|645646|4546|654112.33.44.55.70789137135670 123|1|456464|645646|4546|654112.33.44.55.70789137153670 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

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015086]
help
Chatterbox?
[ambrus]: "you are far more likely to be squished between two other dudes" => wait, really? aren't most planes, weighted by people traveling on it, so narrow that there are only two or three seats on each side of the one corridor, so only one third of the people ge
[ambrus]: t squished between two other people?
holli is about to take the first hit since a month and this weed is strong shit. Should my head explode, someone call 911.
[ambrus]: And even when I traveled on a big plane for a long flight, it had 3 seats, then corridor, then 4 seats, then corridor, then 3 seats, so less than half of the people sit between two other dudes.

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2017-12-18 13:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (486 votes). Check out past polls.

    Notices?