http://www.perlmonks.org?node_id=984659


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)