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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.