Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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?
[Corion]: Hi Discipulus!
[marto]: hopefully you guys are also well :)
[Corion]: marto: Yeah - I'm looking for a week off in a week, even if it'll be a trip to Amsterdam ;)
[Discipulus]: oh thanks, i'm always at well as the ratchet or bucket
[Corion]: But I'm slowly starting to get into programming again - yesterday I translated my talk for YAPC::E (in Amsterdam) to English, and decided to put my Jekyll-clone to rest for the time being
[Corion]: Discipulus++ # being well is like being earnest
[marto]: hopefully next year I'll be in a position to go to Perly things in Europe :)
[Corion]: Maybe I'll use the Real Jekyll a bit to get a feel for how it actually works, and to get annoyed by it ;)
[Corion]: marto: That would be cool :) There is no kids care at Perly events unfortunately, so it's not entirely family friendly
[Corion]: I think I should define a set of (say) five projects through which I rotate from time to time, just to prevent myself from being bored/annoyed by the problems they get stuck in ;)

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2017-07-27 07:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (404 votes). Check out past polls.