Skip to content

Instantly share code, notes, and snippets.

@acviana
Created July 21, 2014 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acviana/b64deeead8fb28030c7d to your computer and use it in GitHub Desktop.
Save acviana/b64deeead8fb28030c7d to your computer and use it in GitHub Desktop.

Unix Commands

How many requests are in the log file?

$ wc example.log
 9999999 173321954 1629465770 example.log

9,999,999 assuming 1 per line.

What percentage of requests from iPads resulted in HTTP status code 500?

$ 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.

What percentage of total bytes sent were in relation to index.html?

$ 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.

How many requests resulted in an HTTP status code of 304?

$ gawk '{print $5}' example.log | grep -c 304
3334467

This is a simpler version of question #2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment