我有一个以下格式的文件:
{" report":[{" call_time":" 2018-03-31 00:10:13"," number" :" 01232802624"" CLI":" 7941232455""名称":空,"目的地":空, "状态":"回答""持续时间":" 27:30"}, {" call_time":" 2018-03-31 00:12:21"," number":" 01233802632"," CLI":" 7831233003""名称":空,"目的地":空,"状态":"回答& #34;"持续时间":" 7:48"}, {" call_time":" 2018-03-31 00:51:16"," number":" 0123802642"," CLI":" 7711123367""名称":空,"目的地":空,"状态":"回答& #34;"持续时间":" 0:57"}, {" call_time":" 2018-03-31 01:50:33"," number":" 012342802624"," CLI":" 7812386544""名称":空,"目的地":空,"状态":"回答& #34;"持续时间":" 9:54"}, {" call_time":" 2018-03-31 16:29:38"," number":" 01232802642"," CLI":" 7741230002""名称":空,"目的地":空,"状态":"回答& #34;"持续时间":" 0:13"}], "总结" {" Total_Calls":" 3862"" Answered_Calls":" 3834"&# 34;回答":" 3922:58"" Calls_Answered":" 99.1%"" ACD":&#34 ; 8:00"}"导致":1}
我需要为每个"数字"过滤除最新十行以外的所有行。 (理想情况下是最新的10项时间),并打印平均持续时间。
预期输出类似于:
2018-03-31 00:10:13 01232802624 27:30
01232802624 Average 27:30
2018-03-31 00:12:21 01233802632 7:48
01233802632 Average 7:48
2018-03-31 00:51:16 0123802642 0:57
2018-03-31 16:29:38 0123802642 0:13
0123802642 Average: 0:30
等
欢迎任何想法...我已经尝试了几个小时使用sed,grep和awk并且无法做到这一点......我的代码和结果到处都是。我很难在网上找到任何解决方案。
答案 0 :(得分:0)
jq是一个处理JSON的强大工具。它在jq Manual提供了很好的文档。
jq对解析持续时间的支持有点缺乏,所以你可能不得不在那里使用其他东西,而且我也不确定你想要的确切输出格式,所以我没有& #39;给出了完整的解决方案。
这是一个例子,也许它有助于推动你朝着正确的方向前进:
$ jq '.report | group_by(.number) | .[][-10:] | [.] | map({number: .[0].number, calls: map({call_time: .call_time, duration: .duration})}) | .[]' < data
{
"number": "01232802624",
"calls": [
{
"call_time": "2018-03-31 00:10:13",
"duration": "27:30"
}
]
}
{
"number": "01232802642",
"calls": [
{
"call_time": "2018-03-31 16:29:38",
"duration": "0:13"
}
]
}
{
"number": "01233802632",
"calls": [
{
"call_time": "2018-03-31 00:12:21",
"duration": "7:48"
}
]
}
{
"number": "012342802624",
"calls": [
{
"call_time": "2018-03-31 01:50:33",
"duration": "9:54"
}
]
}
{
"number": "0123802642",
"calls": [
{
"call_time": "2018-03-31 00:51:16",
"duration": "0:57"
}
]
}
说明:
.report
:获取根对象的report
键group_by(.number)
:按number
键.[][-10:]
:对于每个组(.[]
),只保留最后10个项目([-10:]
)[.]
:在数组中嵌套以使下一个命令开心map(...)
:将组数组映射到对象数组.[]
删除不必要的嵌套这是另一种变体,输出制表符分隔:
$ jq -r '.report | group_by(.number) | .[][-10:] | map([.number, .call_time, .duration]) | .[], [] | join("\t")' < data
01232802624 2018-03-31 00:10:13 27:30
01232802642 2018-03-31 16:29:38 0:13
01233802632 2018-03-31 00:12:21 7:48
012342802624 2018-03-31 01:50:33 9:54
0123802642 2018-03-31 00:51:16 0:57
说明:
.report
:获取根对象的report
键group_by(.number)
:按number
键.[][-10:]
:对于每个组(.[]
),只保留最后10个项目([-10:]
)map(...)
:将对象映射到数组项.[], []
:添加一个额外的数组以在组之间创建空间join("\t")
:使用标签答案 1 :(得分:-1)
我没有时间编写整个脚本,但这可以通过简单的脚本完成:
希望这个帮助