Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

To find and count a repeating pattern in a line

by punitpawar (Sexton)
on Feb 08, 2016 at 19:24 UTC ( [id://1154665]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,
I wanted to know if there is any way by using regular expression to count the occurrences of a repeating pattern in a line ?
here is the problem statement
Given a log file:
some garbage...from:123.54,78.21...more garbage..to:56,82,124.54...more some more garbage...from:11.54,45.84...garbage..to:115.87,98.65 ... Assumption: these coordinates will always appear in sequence: from ... to... from ... to... But these from - to pair may or may not be on same line
Write a script to return pairs of (from, to) coordinates.
The only way I could think of was by splitting the line....But I feel there should be a better way. here is my code below
#!/usr/bin/perl use Data::Dumper; open(FH,"<abc.txt")or die EXPR; while (<FH>) { my @words = split(/ /,$_); foreach (@words) { if ($_=~/from:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/) { print "$1- \n"; }elsif ($_ =~/to:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/){ print "$1 \n"; } } } close (FH);
This is the sample input file abc.txt
hshhhgljlkjglgkjj from:198.18.66.5 aegighighighilg to:198.18.66.6 iiuf +duifuiuih from:198.18.66.7 hiihhj to:198.18.66.8 hhouhoho from:198.18.66.9 igilgojhjh to:198.18.6 +6.10 igighliho from:198.18.66.11 highighioouhouhhhoh to:198.18.66.12
And the output I am expecting is
198.18.66.5 ,198.18.66.6 198.18.66.7 ,198.18.66.8 198.18.66.9 ,198.18.66.10 198.18.66.11 ,198.18.66.12

Replies are listed 'Best First'.
Re: To find and count a repeating pattern in a line
by poj (Abbot) on Feb 08, 2016 at 20:05 UTC
    #!perl use strict; my @from =(); my @to =(); my $re = qr/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/; while (<DATA>){ next unless /(?:to|from):$re/; push @from,($_ =~ /from:($re)/g); push @to, ($_ =~ /to:($re)/g); } for (@from){ print "$_ ,".(shift @to)."\n" } __DATA__ hshhhgljlkjglgkjj from:198.18.66.5 aegighighighilg to:198.18.66.6 iiuf +duifuiuih from:198.18.66.7 hiihhj to:198.18.66.8 hhouhoho from:198.18.66.9 igilgojhjh to:198.18.6 +6.10 igighliho from:198.18.66.11 highighioouhouhhhoh to:198.18.66.12
    poj
Re: To find and count a repeating pattern in a line
by neilwatson (Priest) on Feb 08, 2016 at 19:47 UTC

    I encourage wisdom seekers to present some sample data and use Test::More to present a better picture of their goal.

    Neil Watson
    watson-wilson.ca

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-20 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found