Return to Snippet

Revision: 15327
at July 1, 2009 01:49 by webapplications


Updated Code
#!/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;
	}
 }
}

Revision: 15326
at July 1, 2009 01:37 by webapplications


Initial Code
#!/bin/awk -f

BEGIN {
 FieldName[""] = 0; #init in case there are no fields
}

{
	FieldName[$12]++; #replace #12 with whatever field you want to count
}

END {
	for (i in FieldName) {
		if (i != "") { #skip the initialized value
		print FieldName[i], i;
	}
 }
}

Initial URL


Initial Description
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

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

Initial Tags


Initial Language
Bash