Count number of times that each API version shows up in log file [closed]

Develop script which receives data from localhost on port 33001 and finds how many times distinct API version is requested and print it on a screen.

Sample log is:

172.32.11.121 736443 /api/2.3
98.134.54.184 182294 /api/2.3
172.32.11.121 736443 /api/2.3/security
203.44.122.18 397832 /api/3.0
172.32.11.121 736443 /api/2.3
98.134.54.184 182294 /api/3.0
172.32.11.121 736443 /api/2.3/logging
172.32.11.121 736443 /api/2.3
71.11.139.205 291187 /api/2.3
172.32.11.121 736443 /api/3.0/security
98.134.54.184 182294 /api/2.3
71.11.139.205 397832 /api/3.0/logging

First field is IP-address, second one is user id and last one is path to resource.

Answer

Assuming you care only about the version and not the whole path:

$ awk -F/ '{count[$3]++} END {for (v in count) print v, count[v]}' file
2.3 8
3.0 4

Splitting fields on /, the version number seems to be the third field in all lines.

Leave a Reply

Your email address will not be published. Required fields are marked *