/ Published in: Bash
Use to quickly list the unique fields in a file AND the number of times the field occurs
e.g. Count how times a resource (eg image) has been resulted in a 404 status by examining the IIS logs.
eg assuming this is the input file: **input.txt** and the source script below is in **count.awk**
2009-06-26 GET /presources/ebooks/images/step3.gif 404
2009-06-26 GET /images/20090000/20094617.gif 404
2009-06-26 GET /presources/ebooks/images/digital_rarrow.gif 404
2009-06-26 GET /images/19480000/19482901.gif 404
2009-06-26 GET /images/33110000/33111321.gif 404
2009-06-26 GET /presources/ebooks/images/step3.gif 404
2009-06-26 GET /presources/ebooks/images/step1.gif 404
2009-06-26 GET /presources/ebooks/images/step3.gif 404
2009-06-26 GET /images/20090000/20094487.gif 404
2009-06-26 GET /presources/ebooks/images/step1.gif 404
executing
awk -f count.awk input.txt
will yield
1 /images/33110000/33111321.gif
2 /presources/ebooks/images/step1.gif
3 /presources/ebooks/images/step3.gif
1 /presources/ebooks/images/digital_rarrow.gif
1 /images/20090000/20094487.gif
1 /images/19480000/19482901.gif
1 /images/20090000/20094617.gif
e.g. Count how times a resource (eg image) has been resulted in a 404 status by examining the IIS logs.
eg assuming this is the input file: **input.txt** and the source script below is in **count.awk**
2009-06-26 GET /presources/ebooks/images/step3.gif 404
2009-06-26 GET /images/20090000/20094617.gif 404
2009-06-26 GET /presources/ebooks/images/digital_rarrow.gif 404
2009-06-26 GET /images/19480000/19482901.gif 404
2009-06-26 GET /images/33110000/33111321.gif 404
2009-06-26 GET /presources/ebooks/images/step3.gif 404
2009-06-26 GET /presources/ebooks/images/step1.gif 404
2009-06-26 GET /presources/ebooks/images/step3.gif 404
2009-06-26 GET /images/20090000/20094487.gif 404
2009-06-26 GET /presources/ebooks/images/step1.gif 404
executing
awk -f count.awk input.txt
will yield
1 /images/33110000/33111321.gif
2 /presources/ebooks/images/step1.gif
3 /presources/ebooks/images/step3.gif
1 /presources/ebooks/images/digital_rarrow.gif
1 /images/20090000/20094487.gif
1 /images/19480000/19482901.gif
1 /images/20090000/20094617.gif
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/bin/awk -f BEGIN { FieldName[""] = 0; #init in case there are no fields } { FieldName[$3]++; #replace #3 with whatever field you want to count } END { for (i in FieldName) { if (i != "") { #skip the initialized value print FieldName[i], i; } } }