JSON形式のサンプルデータがあります。
[
{
"info":{
"disk_num":"2",
"hostname":"hostA",
"net":[
"eth0",
"eth1"
],
"software":{
"version":"1.0.0",
"build_date":"2021-10-08",
"commit":"bfdaee"
},
"kernel":"5.0.0"
},
"ip":"10.240.82.192",
"status":"PASS"
},
{
"info":{
"disk_num":"2",
"hostname":"hostA",
"net":[
"eth0",
"eth1"
],
"software":{
"version":"1.0.0",
"build_date":"2021-10-08",
"commit":"bfdaee"
},
"kernel":"5.0.3"
},
"ip":"10.240.82.193",
"status":"PASS"
},
{
"info":{
"disk_num":"5",
"hostname":"hostB",
"net":[
"eth0",
"eth1"
],
"software":{
"version":"1.0.2",
"build_date":"2021-10-11",
"commit":"935678"
},
"kernel":"5.0.1"
},
"ip":"10.240.82.194",
"status":"PASS"
}
]
部分データを取得して別のjson FORMATを設定したいと思います。
[
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
},
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
},
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.1"
}
]
このコマンドを試しました(サンプルデータをjson.dataに保存しました)。
cat json.data|jq -r '{IP:.[].ip,STATUS:.[].status,SOFTWARE:.[].info.software.version,KERNEL:.[].info.kernel}'
動作せず、次のように出力されます。
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.1"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.0"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.3"
}
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.1"
}
私が望むものを得るための正しいcliコマンドを知りたいです。
非常にありがとう
答え1
指定した4つの要素のそれぞれに対して、コマンドが各配列要素に対して繰り返されるため、3つではなく3 4 = 81個のオブジェクトが生成されます。
各オブジェクトを繰り返すには、map()
次の関数を使用できます。
jq 'map({IP:.ip, STATUS:.status, SOFTWARE:.info.software.version, KERNEL:.info.kernel})' json.data
[
{
"IP": "10.240.82.192",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.0"
},
{
"IP": "10.240.82.193",
"STATUS": "PASS",
"SOFTWARE": "1.0.0",
"KERNEL": "5.0.3"
},
{
"IP": "10.240.82.194",
"STATUS": "PASS",
"SOFTWARE": "1.0.2",
"KERNEL": "5.0.1"
}
]
答え2
@roaimaの答えはこれをよく説明したり、次のように解決策を書くことができます。
jq '[ .[] | {IP:.ip, STATUS:.status, SOFTWARE:.info.software.version, KERNEL:.info.kernel} ]' json.data
ここ.[]
で、部分は入力のすべての要素に対して繰り返されますが、結果[]
全体をラップする外部部分はそれを配列に変換します。