In general when processing log files, "slurping" or reading the whole file into a single $var is a bad idea. Usually better is to process the log file one line at a time. One reason is so that your code doesn't depend upon the size of the log file. Another reason is for exactly the problem that you are experiencing, the regex stuff gets more complex. There are also some issues about dealing with corrupted lines or records and such things. But that is not relevant to your current problem.
I don't know where you heard about:
my $lines = do { local $/; <STATFILE> };, but that uses two relatively rare constructs in the same statement! It is highly likely that you can write Perl code for a number of years and never need either "do" or "local".
Below, I wrote a simple parser for you. There is a predefined handle called DATA which I used instead of opening an external file.
The code loops on each line of DATA and searches for a line that ends with "Client Address = some_ip_address". Then the code calls get_units_rcvd() to get that number and the results are printed. That's it. Well code does loop and do the same thing again!
The subroutine, get_units_rcvd() could be written more compactly as could all of this code. But more compact doesn't mean "faster" and there is no need here. This is a demo of a common methodology. Look for something that "starts the record" and then call a subroutine to complete the job.
#!/usr/bin/perl -w use strict; while (<DATA>) { if (m/Client Address = ([0-9.]+)\s*$/) { my $ip_adr = $1; my $units_rcvd = get_units_rcvd(); print "$ip_adr => $units_rcvd\n"; } } sub get_units_rcvd { while (<DATA>) { if (/Data Units Received = ([0-9]+)\s*$/) { return ( $1); } } } =prints: 192.0.0.6 => 260 192.0.0.4 => 351 192.0.0.5 => 207 192.0.0.2 => 401 192.0.0.3 => 340 =cut
__DATA__ 1, , , Transport, TCP,Total Packets with E +rrors = 0 1, , , Transport, TCP,Packets Received w +ith Checksum Errors = 0 1, , , Transport, TCP,Packets Received w +ith Bad Offset = 0 1, , , Transport, TCP,Packets Received t +hat are Too Short = 0 1, ,[1024], Application,TRAFFIC-GEN Server,Client Add +ress = 192.0.0.6 1, ,[1024], Application,TRAFFIC-GEN Server,Session St +art Time (s) = 0.226811597 1, ,[1024], Application,TRAFFIC-GEN Server,Session En +d Time (s) = 19.909754409 1, ,[1024], Application,TRAFFIC-GEN Server,Session St +atus = Closed 1, ,[1024], Application,TRAFFIC-GEN Server,Total byte +s Received = 8320 1, ,[1024], Application,TRAFFIC-GEN Server,Total Data + Units Received = 260 1, ,[1024], Application,TRAFFIC-GEN Server,Throughput + (bits/s) = 3381 1, ,[1024], Application,TRAFFIC-GEN Server,Average En +d-to-End Delay (s) = 0.026102975 1, ,[1024], Application,TRAFFIC-GEN Server,Average Ji +tter (s) = 0.070900925 1, ,[1024], Application,TRAFFIC-GEN Server,Client Add +ress = 192.0.0.4 1, ,[1024], Application,TRAFFIC-GEN Server,Session St +art Time (s) = 0.107537260 1, ,[1024], Application,TRAFFIC-GEN Server,Session En +d Time (s) = 19.984968334 1, ,[1024], Application,TRAFFIC-GEN Server,Session St +atus = Closed 1, ,[1024], Application,TRAFFIC-GEN Server,Total byte +s Received = 11232 1, ,[1024], Application,TRAFFIC-GEN Server,Total Data + Units Received = 351 1, ,[1024], Application,TRAFFIC-GEN Server,Throughput + (bits/s) = 4520 1, ,[1024], Application,TRAFFIC-GEN Server,Average En +d-to-End Delay (s) = 0.020293675 1, ,[1024], Application,TRAFFIC-GEN Server,Average Ji +tter (s) = 0.056769093 1, ,[1024], Application,TRAFFIC-GEN Server,Client Add +ress = 192.0.0.5 1, ,[1024], Application,TRAFFIC-GEN Server,Session St +art Time (s) = 0.058634166 1, ,[1024], Application,TRAFFIC-GEN Server,Session En +d Time (s) = 19.798208565 1, ,[1024], Application,TRAFFIC-GEN Server,Session St +atus = Closed 1, ,[1024], Application,TRAFFIC-GEN Server,Total byte +s Received = 6624 1, ,[1024], Application,TRAFFIC-GEN Server,Total Data + Units Received = 207 1, ,[1024], Application,TRAFFIC-GEN Server,Throughput + (bits/s) = 2684 1, ,[1024], Application,TRAFFIC-GEN Server,Average En +d-to-End Delay (s) = 0.026288118 1, ,[1024], Application,TRAFFIC-GEN Server,Average Ji +tter (s) = 0.090685171 1, ,[1024], Application,TRAFFIC-GEN Server,Client Add +ress = 192.0.0.2 1, ,[1024], Application,TRAFFIC-GEN Server,Session St +art Time (s) = 0.028508654 1, ,[1024], Application,TRAFFIC-GEN Server,Session En +d Time (s) = 19.981800333 1, ,[1024], Application,TRAFFIC-GEN Server,Session St +atus = Closed 1, ,[1024], Application,TRAFFIC-GEN Server,Total byte +s Received = 12832 1, ,[1024], Application,TRAFFIC-GEN Server,Total Data + Units Received = 401 1, ,[1024], Application,TRAFFIC-GEN Server,Throughput + (bits/s) = 5144 1, ,[1024], Application,TRAFFIC-GEN Server,Average En +d-to-End Delay (s) = 0.009312223 1, ,[1024], Application,TRAFFIC-GEN Server,Average Ji +tter (s) = 0.046495978 1, ,[1024], Application,TRAFFIC-GEN Server,Client Add +ress = 192.0.0.3 1, ,[1024], Application,TRAFFIC-GEN Server,Session St +art Time (s) = 0.017999448 1, ,[1024], Application,TRAFFIC-GEN Server,Session En +d Time (s) = 19.943126887 1, ,[1024], Application,TRAFFIC-GEN Server,Session St +atus = Closed 1, ,[1024], Application,TRAFFIC-GEN Server,Total byte +s Received = 10880 1, ,[1024], Application,TRAFFIC-GEN Server,Total Data + Units Received = 340 1, ,[1024], Application,TRAFFIC-GEN Server,Throughput + (bits/s) = 4368 1, ,[1024], Application,TRAFFIC-GEN Server,Average En +d-to-End Delay (s) = 0.019138684 1, ,[1024], Application,TRAFFIC-GEN Server,Average Ji +tter (s) = 0.060386082 2, , [0], Physical, 802_15_4,Signals transmitte +d = 1571 2, , [0], Physical, 802_15_4,Signals detected = + 1732 2, , [0], Physical, 802_15_4,Signals locked on +by PHY = 1191
In reply to Re: multiple regexp matches in multi line string
by Marshall
in thread multiple regexp matches in multi line string
by wirelesscharlie
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |