views:

67

answers:

2

Say I have a file of this format

12:04:21  .3
12:10:21  1.3
12:13:21  1.4
12:14:21  1.3
..and so on

I want to find repeated numbers in the second column for, say, 10 consequent timestamps, thereby finding staleness.

12:04:21  .3
12:10:21  1.3
12:14:21  1.3
12:10:21  1.3
12:14:21  1.3
12:12:21  1.3
12:24:21  1.3
12:30:21  1.3
12:44:21  1.3
12:50:21  1.3
13:04:21  1.3
13:24:21  1.7

should print 12:10:21 through 13:04:21 1.3

and I want to output the beginning and and end of the stale timestamp range

Can someone help me come up with it?

You can use awk, bash

Thanks

+1  A: 
awk 'BEGIN { count = 1} { if ( $2 == prev ) { ++count; if ( ! start ) {start = prevtime} end = $1 } 
       else if ( count >= 10 ) { print start, end, prev; count = 1; start = "" }
       else { start = "" }; 
       prev = $2; prevtime = $1 }' file.dat

Edit 2:

Found and fixed another bug.

Dennis Williamson
doesnt work it prints for every column
vehomzzz
@Andrei: I don't understand what you mean. Do you mean "every row"? In any case, I found a bug and fixed it. Try the edited version.
Dennis Williamson
A: 

Here is my version, which is more verbose:

# This function prints out the summary only when count >= 10
function print_summary(count, first, last, value) {
    if (count >= 10) {
        printf "%s through %s %s (%d)\n", first, last, last_value, count
    }
}

$2 == last_value {
    last_occurance = $1
    count++
}

$2 != last_value {
    print_summary(count, first_occurance, last_occurance, last_value)
    first_occurance = $1
    last_value = $2
    count = 1
}

END { 
    print_summary(count, first_occurance, last_occurance, last_value)
}
Hai Vu