我有这个从我的Web服务器获取顶级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中。
单个值可以正常工作,但可以解析我遇到麻烦的拓扑列表。
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
}
答案 0 :(得分:1)
让我们从问题描述中的文本行开始:
192.168.1.2
192.168.1.3
192.168.1.5
192.168.1.6
192.168.1.7
如果count = 0,则将这些行通过管道传递到jq程序的此变体中:
jq -nR --arg stat fails --arg count $count '
[{$stat,$count},
{liststat: "topips",
items: [inputs]}]'
您将获得如下所示的输出。您应该能够根据需要修改此示例。您有可能单独使用jq即可有效地获得所需的输出(即,没有Q中显示的任何中间步骤)。
[
{
"stat": "fails",
"count": "0"
},
{
"liststat": "topips",
"items": [
"192.168.1.2",
"192.168.1.3",
"192.168.1.5",
"192.168.1.6",
"192.168.1.7"
]
}
]