开发者

Determine stale data

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 findi开发者_运维技巧ng 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


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.


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)
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜