#!/usr/bin/perl

use warnings;

# this is a quick and dirty way of dhoing this - it just demonstrates how you can use
# propoerties of a file to separate information out.

setpriority 0, 0, 10;
select(STDOUT);
$| = 1;

##########################################
$n = 0; #count the number of lines in the log file
#slurp log file
open (LOG, "access_log");
        while (<LOG>) {
                $n++;
                chomp; #remove line endings
                $a = $_; #place into a normal scalar
                @z = split " ", $a; #split it into its parts using the space
                $j = "@z[(11..$#z)]";
                # Join the elements in a space separated list, re-forming the User Agent.
                # We are now going to use an associtive array called a hash.
                # This has one scalar as the key (analogous to the index in a normal array)
                # and another scalar as the data. We are going to use it to store the
                # User Agent as a key so that any duplicates are removed.
                # We are also going to use the value so that we can find out how many of each
                # one there is.
                if (exists $h{$j}) {
                  #it already exists so let's add one to its value.
                  $h{$j}++;
                } else {
                  #It's a new one so let's start it off with one.
                  $h{$j} = 1;
                }
        }
close (LOG);

# Sort the keys and store them in an array
@kl = sort (keys (%h));

# write sorted file
open (SRT, ">ua.txt");
        foreach $y (@kl) {
                print SRT "$h{$y} $y\n";
        }
close (SRT);

#now output some stats to the console.
# $n is the total number of lines/ Let's tunr it into a human-readable value
1 while ($n =~ s/(\d+)(\d\d\d)/$1,$2/);
# The number of elements in the key list array (@kl) is the number we have left.
$m = $#kl + 1;
1 while ($m =~ s/(\d+)(\d\d\d)/$1,$2/);
#print it out. Remember that '\n' is a new line.
print "Processed\n $n records.\n $m unique UserAgents.\n";
