Awk Script to count # of occurences of fields in a file


/ Published in: Bash
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. #!/bin/awk -f
  2.  
  3. BEGIN {
  4. FieldName[""] = 0; #init in case there are no fields
  5. }
  6.  
  7. {
  8. FieldName[$3]++; #replace #3 with whatever field you want to count
  9. }
  10.  
  11. END {
  12. for (i in FieldName) {
  13. if (i != "") { #skip the initialized value
  14. print FieldName[i], i;
  15. }
  16. }
  17. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.