Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://984659]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2022-08-13 23:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found