#
# countos.awk, 22 Jan 13

# Extract User Agent string from server log file

	{
	# User agent is between double quotes, so split on this character
	l=split($0, quoted, "\"")
	# Nothing to do if no user agent present
	if (l < 6)
	   next
	# Grab everything between first pair of brackets
	open_b=index(quoted[6], "(")
	close_b=index(quoted[6], ")")
	if ((open_b == 0) || (close_b == 0))
	   next
	# We hang on to more than is needed, sort things out later
	os_str=substr(quoted[6], open_b+1, close_b-open_b-1)
	# Not interested in bots
	is_http=index(os_str, "+http")
	if (is_http != 0)
	   next
	if ((os_str == "compatible;") || (os_str == "compatible"))
	   next
	# Save some space by removing known unnecessary information
	sub(/MSIE [0-9.bcd]+; /, "", os_str)
	sub(/MSIE [0-9.bcd]+$/, "", os_str)
	sub(/rv:[0-9.]+/, "", os_str)
	while (sub(/; .NET CLR [0-9.]+/, "", os_str) == 1)
	   ; # do nothing
	sub(/compatible; /, "", os_str)
	sub(/ U; /, "", os_str)
	sub(/X11; /, "", os_str)
	sub(/X11;/, "", os_str)
	# Date/time field is [06/Jun/2012:00:15:38
	# Keep month and year
	acc_date=substr($4, 5, 8)
	date_os=acc_date " " os_str
	if (os_cnt[date_os] == "") # have we seen this before?
	   os_cnt[date_os]=1
	else
	   os_cnt[date_os]++
	next
	}

END {
	for (os in os_cnt)
	   print os_cnt[os] " " os
	}

