bt) && -6ren">
gpt4 book ai didi

linux - 从 awk 输出中计算唯一值

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:41:18 25 4
gpt4 key购买 nike

我想知道在过去 30 分钟内有多少用户使用我的代理访问过 google.com。

 awk -v bt=$(date "+%s" -d "30 minutes ago") '($1 > bt) && $4~/google.com/ {printf("%s|%s|%s|%s\n", strftime("%F %T",$1), $2 , $3, $4)} ' access.log

日志是这样的

2017-02-19 12:09:44|test@gmail.com|200|https://google.com/
2017-02-19 12:10:23|test@gmail.com|200|https://google.com/

现在我可以很容易地统计记录的数量

 awk -v bt=$(date "+%s" -d "30 minutes ago") '($1 > bt) && $4~/google.com/ {printf("%s|%s|%s|%s\n", strftime("%F %T",$1), $2 , $3, $4)} ' access.log | wc -l

输出为 2。

如何修改命令以仅显示具有唯一电子邮件的记录。在上述情况下,输出应为 1。

最佳答案

列出结果

awk -v FS='|' -v bt="$(date +'%Y-%m-%d %H:%M:%S' -d '30 minutes ago')" '
($1 > bt) && $4~/google.com/ && !seen[$2]++
' access.log

获取计数

awk -v FS='|' -v bt="$(date +'%Y-%m-%d %H:%M:%S' -d '30 minutes ago')" '
($1 > bt) && $4~/google.com/ && !seen[$2]++{ count++ }
END{ print count+0 }
' access.log

用于测试

# Current datetime of my system
$ date +'%Y-%m-%d %H:%M:%S'
2017-02-26 00:06:19

# 30 minutes ago what was datetime
$ date +'%Y-%m-%d %H:%M:%S' -d '30 minutes ago'
2017-02-25 23:36:20

# Input file, I modified datetime to check command
$ cat f
2017-02-25 23:10:44|test@gmail.com|200|https://google.com/
2017-02-25 23:45:23|test@gmail.com|200|https://google.com/

输出-1看结果

$ awk -v FS='|' -v bt="$(date +'%Y-%m-%d %H:%M:%S' -d '30 minutes ago')" '
($1 > bt) && $4~/google.com/ && !seen[$2]++
' f
2017-02-25 23:45:23|test@gmail.com|200|https://google.com/

输出 - 2 查看计数

$ awk -v FS='|' -v bt="$(date +'%Y-%m-%d %H:%M:%S' -d '30 minutes ago')" '
($1 > bt) && $4~/google.com/ && !seen[$2]++{ count++ }
END{ print count+0 }
' f
1

关于linux - 从 awk 输出中计算唯一值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42459599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com