Finding minimum maximum and average

I am trying to find the minimum maximum and average from one file which has values

Received message from [https://www.demandmatrix.net/app/dm/xml] in [939] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [981] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [447] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [93] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [123] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [124] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [89] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [124] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [103] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [167] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [179] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [2498] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [2505] milliseconds.

I am using the following command

awk 'NR==1{min=$6;max=$6;sum+=$6;next}{if ($6<min) min=$6;if ($12>max) max=$6;sum+=$6}END{print "min: "min", max: "max",NR: "NR", avg: "sum/NR}' filename

but due to square braces I am not able to run this command please suggest how to get the output

Why is there a

$12

in that formula?

awk ' BEGIN { max=sum=0; } NR==1 { min=$(NF-1); } {
 val=$(NF-1);
 gsub("\[","",val);
 gsub("\]","",val);
 sum+=val;
 if(val<min) { min=val; }
 if(val>max) { max=val; }
 } END {
 printf "Min: %d Max: %d Avg: %d\n", min, max, (sum/NR);
} ' filename
1 Like

The following strips out the [ and ] characters. There are most assuredly other ways also.

$ cat sample32.txt
Received message from [https://www.demandmatrix.net/app/dm/xml] in [939] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [981] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [447] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [93] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [123] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [124] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [89] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [124] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [103] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [167] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [179] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [2498] milliseconds.
Received message from [https://www.demandmatrix.net/app/dm/xml] in [2505] milliseconds.

$ tr -d '[\[\]]' <sample32.txt | awk -F" " '{print $6}'
939
981
447
93
123
124
89
124
103
167
179
2498
2505
awk 'BEGIN {minval=999999;maxval=-1} {val=$6;gsub(/[^0-9]/,"",val);if (val < minval) minval=val; if (val > maxval) maxval=val; sum=sum+val;} END {print "min " minval;print "max " maxval;print "avg " sum/NR;}'

Feel free to adjust.

or simply:

$ awk -F'[][]' '{print $(NF-1)}' myFile.txt
939
981
447
93
123
124
89
124
103
167
179
2498
2505