linux - 从日志文件中计算具有匹配模式的行

标签 linux shell unix awk grep

我的 abc.log 包含以下条目(片段):

...
INFO #my-service# #add# id=67986324423 isTrial=true
INFO #my-service# #add# id=43536343643 isTrial=false
INFO #my-service# #add# id=43634636365 isTrial=true
INFO #my-service# #add# id=67986324423 isTrial=true
INFO #my-service# #delete# id=43634636365 isTrial=true
INFO #my-service# #delete# id=56543435355 isTrial=false
...

我想计算具有唯一 ID 且其中包含 #add# 属性并具有 isTrial=true 的行数。

对于上面的代码片段,输出应该是 2

任何人都可以提供我可以针对上述日志文件运行的 linux 命令吗?

最佳答案

仅使用 awk:

# Count unique line
$ awk '$3~"add"&&$5~"true"&&!u[$4]++{++c}END{print c}' file
2

# Print unique lines
$ awk '$3~"add"&&$5~"true"&&!u[$4]++' file
INFO #my-service# #add# id=67986324423 isTrial=true
INFO #my-service# #add# id=43634636365 isTrial=true

或者只是sortgrep

$ sort -uk4,4 file | grep "#add#.*true" 
INFO #my-service# #add# id=67986324423 isTrial=true
INFO #my-service# #add# id=43634636365 isTrial=true

$ sort -uk4,4 file | grep -c "#add#.*true"
2

关于linux - 从日志文件中计算具有匹配模式的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14266152/

相关文章:

linux - 验证字符串并确保它由特定字符组成

php - .htaccess 帮助 - 带有特殊字符的 301 重定向 URL

bash - Scanimage 无法从变量获取参数

sockets - 如何使用netcat发送文件并保持连接状态?

linux - 我如何在 grep 中创建相同输出的类?

linux - 映射到虚拟地址空间的内核符号表——为什么?

c++ 套接字关闭第一次连接尝试

c - 如何在c开发过程中避免内存泄漏

linux - 如何在 bash 中使用 grep 测试进程是否正在运行?

bash - LINES 和 COLUMNS 环境变量在脚本中丢失