Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Can I count lines in a substring?

by suaveant (Parson)
on Jul 31, 2012 at 20:29 UTC ( #984659=note: print w/replies, xml ) Need Help??

in reply to Can I count lines in a substring?

Certainly you can, and there are any number of ways to do it. One of the simplest might be to read in the whole file and split it on /^Traceroute:/xms and work with the chunks, but if the file is large (or could be large in the future) that may not be the best method. Going with what you have now I'd say do something like...
while (my $line = <$in>) { if($line =~ /^traceroute to (\S+)/) { $current_traceroute = $1; $count = 0; #reset the count; print "Trace for $current_traceroute: "; } elsif($line =~ /^\s*\d+\s+/) { $count++; } elsif($line =~ /^Query:/) { print "$count\n"; } }
Basically you want to catch the beginning (and end) of each block and reset things to start a new count and/or pull any other relevant data. If Query: isn't reliably there then you might have to rely on "Traceroute:" or "traceroute to" to signal the end of the previous call, but then you'd have to handle the last route by triggering on eof since there would be no final "Traceroute:". Make sense?

                - Ant
                - Some of my best work - (1 2 3)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://984659]
[vedagiri89]: how do fix this issue
[choroba]: This usually happens when you have more than one version of Perl, and you mix the libraries
[hippo]: Which O/S?
[vedagiri89]: i have searched many forum...not getting resolutions
[vedagiri89]: centos
[Corion]: vedagiri89: Either what choroba said, or maybe you misspelled some module name (on Windows), like use Strict; or something like that.
[vedagiri89]: @choroba, what to do for that
[Corion]: Ah, you are not on Windows, so disregard my opinion.
[vedagiri89]: in centos, how to fix this.
[choroba]: You have to find out why it happened. Was there an OS update?

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (13)
As of 2018-06-19 11:21 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (113 votes). Check out past polls.