Question

I've got the following logfile and I'd like to extract the number of dropped packets (in the following example the number is 0):

ITGDec version 2.8.1 (r1023)
Compile-time options: bursty multiport
----------------------------------------------------------
Flow number: 1
From 192.168.1.2:0
To    192.168.1.2:8999
----------------------------------------------------------
Total time               =      2.990811 s
Total packets            =           590
Minimum delay            =      0.000033 s
Maximum delay            =      0.000169 s
Average delay            =      0.000083 s
Average jitter           =      0.000010 s
Delay standard deviation =      0.000016 s
Bytes received           =        241900
Average bitrate          =    647.048576 Kbit/s
Average packet rate      =    197.270907 pkt/s
Packets dropped          =             0 (0.00 %)
Average loss-burst size  =      0.000000 pkt
----------------------------------------------------------

__________________________________________________________
****************  TOTAL RESULTS   ******************
__________________________________________________________
Number of flows          =             1
Total time               =      2.990811 s
Total packets            =           590
Minimum delay            =      0.000033 s
Maximum delay            =      0.000169 s
Average delay            =      0.000083 s
Average jitter           =      0.000010 s
Delay standard deviation =      0.000016 s
Bytes received           =        241900
Average bitrate          =    647.048576 Kbit/s
Average packet rate      =    197.270907 pkt/s
Packets dropped          =             0 (0.00 %)
Average loss-burst size  =             0 pkt
Error lines              =             0
----------------------------------------------------------

I'm trying with the following command:

cat logfile | grep -m 1 dropped | sed -n 's/.*=\([0-9]*\) (.*/\1/p'

but nothing gets printed.

Thank you

EDIT: I just wanted to tell you that the "Dropped packets" line gets printed in the following way in the code of the program:

printf("Packets dropped          = %13lu (%3.2lf %%)\n", (long unsigned int) 0, (double) 0);
Was it helpful?

Solution 2

Aside from the problem in your sed expression (that it doesn't allow space after =), you don't really need a pipeline here.

grep would suffice:

grep -m 1 -oP 'dropped\s*=\s*\K\d+' logfile

You could have fixed your sed expression by permitting space after the =:

sed -n 's/.*= *\([0-9]*\) (.*/\1/p'

OTHER TIPS

It will be easier to use awk here:

awk '/Packets dropped/{print $4}' logfile

Avoiding your use of cat and grep, in plain sed:

sed -n 's/^Packets dropped[=[:space:]]\+\([0-9]\+\).*/\1/p' logfile

Matches

  • any line starting with "Packets dropped"
  • one or more whitespace or "=" characters
  • one or more digits (which are captured)

The rest .* is discarded.

With the -r option as well, you can lose a few backslashes:

sed -nr 's/^Packets dropped[=[:space:]]+([0-9]+).*/\1/p' logfile
sed -n '/Packets dropped/ s/.*[[:space:]]\([0-9]\{1,\}\)[[:space:]].*/\1/p' YourFile

but print both 2 line (detail + summary) where info is write

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top