【发布时间】:2020-10-30 00:39:02
【问题描述】:
我有这个从我的网络服务器获取顶级 IP。试图返回在底部看到的 JSON 对象。是否可以轻松地将行转换为 jq 创建字符串数组所需的格式?
ips=$(cat /var/log/nginx/access.log | cut -d' ' -f1 | sort | uniq -c | sort -nr | head -n 5 | cut -d' ' -f8)
结果:
192.168.1.2
192.168.1.3
192.168.1.5
192.168.1.6
192.168.1.7
我的预期输出是:
"192.168.1.2" "192.168.1.3" "192.168.1.5" "192.168.1.6" "192.168.1.7"
我可能会有很长的路要走。我所拥有的是这个输出进入一个带有其他值的 json 文件的 jq。
单个值工作正常,但解析我遇到问题的 topipsis 列表。
jq -n --arg stat fails --arg count $count '[{"stat":$stat,"count": $count},{"liststat": "topips",items: $ARGS.positional }]' --args ${ips[@]}
预期结果如下:
{
"topip": ["10.10.20.9","10.10.10.24","10.10.10.26","10.10.10.28","10.10.10.121","192.168.1.152","172.169.10.21","112.10.10.2","10.10.10.21","10.10.10.21"],
"logins":66,
"visits":75,
"errors":1759
}
【问题讨论】:
-
那个 jq 位试图创建一个对象数组,但您的预期输出是单个对象?
-
请遵循minimal reproducible example 准则。问题的各个组成部分之间的联系有时看起来相当不透明。