确定陈旧数据

时间:2010-06-03 19:12:54

标签: bash scripting awk

说我有这种格式的文件

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

我想在第二列中找到重复的数字,例如10个随后的时间戳,从而找到陈旧性。

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

应打印 12:10:21至13:04:21 1.3

我希望输出陈旧时间戳范围的开头和结尾

有人可以帮我提出来吗?

你可以使用awk,bash

由于

2 个答案:

答案 0 :(得分:1)

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

编辑2:

找到并修复了另一个错误。

答案 1 :(得分:0)

这是我的版本,更详细:

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