确定陈旧数据

发布于 2024-09-03 17:57:06 字数 488 浏览 1 评论 0原文

假设我有一个这种格式的文件,

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假扮的天使 2024-09-10 17:57:06
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:

发现并修复了另一个错误。

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.

红墙和绿瓦 2024-09-10 17:57:06

这是我的版本,更详细:

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

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)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文