开发者

Determine stale data

开发者 https://www.devze.com 2023-01-02 09:22 出处:网络
Say I have a file of this format 12:04:21.3 12:10:211.3 12:13:211.4 12:14:211.3 ..and so on I want to findrepeated numbersin the second columnfor, say, 10 consequent timestamps, thereby findi开发者

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

精彩评论

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