$ wc example.log
9999999 173321954 1629465770 example.log
9,999,999 assuming 1 per line.
$ grep -c iPad example.log
1110756
First we grep
each line for the pattern iPad
to get the total number of requests from an iPad. My command assumes a consistent capitalization of the the word iPad
but we could avoid that by using the -i
flag.
$ grep iPad example.log | gawk '{print $5}' | grep -c 500
369882
Then to avoid matching a grep
pattern for 500 on requests that transfered 500 bytes we gawk
out the column of interest, finally running grep
for the value 500
with the -c
flag to count the results.
$ bc <<< "scale=2;369882/1110756"
.33
Finally, to really keep things in the shell, we can use the bc
utility to compute the percentage.
$ gawk '{sum += $6} END {print sum}' example.log
5324868718
This time we use gawk
to find the some the 6th column, the bytes sent.
$ grep "/index.html\t" example.log | gawk '{sum += $6} END {print sum}'
295927182
Then we do the same thing but first grep out the requests to index.html
using the \t
tab character to remove requests like index.html?1
which I assumed should be outside of the scope of the query.
$ bc <<< "scale=2;295927182/5324868718"
.05
Finally, we use bc
to get the percentage.
$ gawk '{print $5}' example.log | grep -c 304
3334467
This is a simpler version of question #2.