views:

68

answers:

2

Suppose I have a file of five lines with IP addresses:

1.2.3.4
5.6.7.8
5.6.7.8
9.9.9.9
5.6.7.8

I need to find the average number of times every IP is repeated consecutively. In this example, every sequence of the same IPs has the length of 1, except for 5.6.7.8, which is repeated twice. So, for this example the answer is (1 + 2 + 1 + 1) / 4 = 1.25.

I'm trying to figure out how many times on average the users clicked the download button on my website before either giving up or realising that they should react to Firefox addon installation warning above the page.

Can this be done in a command line or I have to write a script to do it? I'm fine with writing a ruby script for it but I wonder if it can be done with a smart oneliner.

+2  A: 

In standard shell I would do it like this:

uniq -c intputfile | awk '{count++;sum+=$1} END{print count,"lines.","Average is:",sum/count}'
Sec
Thank you! uniq -c is exactly what I didn't know
Evgeny Shadchnev
Very nice :) +1
jkp
A: 

Quick and dirty:

echo `cat test.txt | wc -l` / `uniq test.txt | wc -l` | bc -l

It's possible you need to install bc first (to do the floating point calculation).

ChristopheD