使用 jq 计算使用的卫星数量,并从 gpsd 输出中查看
Using jq to count number of satellites used and in view from gpsd output
考虑这个 JSON:
{"class":"POLL","time":"2020-08-24T07:01:31.690Z","active":1,"tpv":[{"class":"TPV","tag":"ID8fac","device":"/dev/ttyM1","mode":3,"time":"2020-08-24T07:01:31.000Z","ept":0.005,"lat":-37.043028622,"lon":145.984996786,"alt":708.492,"epx":9.963,"epy":14.642,"epv":0.000,"speed":0.000,"climb":0.000,"eps":29.28}],"gst":[{"class":"GST","tag":"ID8fac","device":"/dev/ttyM1","time":"1970-01-01T00:00:00.000Z","rms":0.000,"major":0.000,"minor":0.000,"orient":0.000,"lat":0.000,"lon":0.000,"alt":0.000}],"sky":[{"class":"SKY","tag":"ID8fac","device":"/dev/ttyM1","xdop":0.66,"ydop":0.98,"vdop":0.00,"tdop":1.00,"hdop":0.00,"gdop":1.00,"pdop":0.00,"satellites":[{"PRN":17,"el":42,"az":137,"ss":43,"used":true},{"PRN":12,"el":27,"az":241,"ss":44,"used":true},{"PRN":24,"el":53,"az":247,"ss":46,"used":true},{"PRN":6,"el":55,"az":54,"ss":36,"used":true},{"PRN":15,"el":10,"az":307,"ss":40,"used":true},{"PRN":2,"el":31,"az":358,"ss":37,"used":true},{"PRN":28,"el":20,"az":89,"ss":35,"used":true},{"PRN":7,"el":10,"az":52,"ss":31,"used":false},{"PRN":19,"el":64,"az":148,"ss":45,"used":true},{"PRN":13,"el":10,"az":338,"ss":29,"used":false},{"PRN":15,"el":18,"az":297,"ss":33,"used":true},{"PRN":22,"el":10,"az":17,"ss":39,"used":false}]}]}
我有兴趣解析上面 JSON 中的卫星数组(下面提取)并返回两个数字:卫星数和使用的卫星数。例如:7 12
[
{
"PRN": 17,
"el": 42,
"az": 137,
"ss": 43,
"used": true
},
{
"PRN": 12,
"el": 27,
"az": 241,
"ss": 44,
"used": true
},
{
"PRN": 24,
"el": 53,
"az": 247,
"ss": 46,
"used": true
},
{
"PRN": 6,
"el": 55,
"az": 54,
"ss": 36,
"used": true
},
{
"PRN": 15,
"el": 10,
"az": 307,
"ss": 40,
"used": true
},
{
"PRN": 2,
"el": 31,
"az": 358,
"ss": 37,
"used": true
},
{
"PRN": 28,
"el": 20,
"az": 89,
"ss": 35,
"used": true
},
{
"PRN": 7,
"el": 10,
"az": 52,
"ss": 31,
"used": false
},
{
"PRN": 19,
"el": 64,
"az": 148,
"ss": 45,
"used": true
},
{
"PRN": 13,
"el": 10,
"az": 338,
"ss": 29,
"used": false
},
{
"PRN": 15,
"el": 18,
"az": 297,
"ss": 33,
"used": true
},
{
"PRN": 22,
"el": 10,
"az": 17,
"ss": 39,
"used": false
}
]
目前我只能弄清楚如何解析一个值,而不是一次解析两个值;这就是我所拥有的:
jq -r '[ .sky[] | .satellites[]| select(.used == true )| "\( .PRN )" ] |length'
输出使用的卫星数量,例如 7。
为了完整起见,如果它可能对某人有所帮助,我将按如下方式在 bash 中轮询 gpsd。此 returns 一个 JSON 结构,如 post 开头所示:
timeout 1 grep -m1 satellites < <(printf '%s\n' '?WATCH={"enable":true,"json":true};?POLL;' | nc 127.0.0.1 2947)
对于呈现的输入:
$ jq -r '.sky[].satellites | ( [ .[] | select(.used == true) ] | length ), length'
9
12
或者也许(但您也可以将其通过管道传输到 paste -sd" "
):
$ jq -r '.sky[].satellites | ( [ .[] | select(.used == true) ] | length | tostring) + " " + (length|tostring)'
9 12
考虑这个 JSON:
{"class":"POLL","time":"2020-08-24T07:01:31.690Z","active":1,"tpv":[{"class":"TPV","tag":"ID8fac","device":"/dev/ttyM1","mode":3,"time":"2020-08-24T07:01:31.000Z","ept":0.005,"lat":-37.043028622,"lon":145.984996786,"alt":708.492,"epx":9.963,"epy":14.642,"epv":0.000,"speed":0.000,"climb":0.000,"eps":29.28}],"gst":[{"class":"GST","tag":"ID8fac","device":"/dev/ttyM1","time":"1970-01-01T00:00:00.000Z","rms":0.000,"major":0.000,"minor":0.000,"orient":0.000,"lat":0.000,"lon":0.000,"alt":0.000}],"sky":[{"class":"SKY","tag":"ID8fac","device":"/dev/ttyM1","xdop":0.66,"ydop":0.98,"vdop":0.00,"tdop":1.00,"hdop":0.00,"gdop":1.00,"pdop":0.00,"satellites":[{"PRN":17,"el":42,"az":137,"ss":43,"used":true},{"PRN":12,"el":27,"az":241,"ss":44,"used":true},{"PRN":24,"el":53,"az":247,"ss":46,"used":true},{"PRN":6,"el":55,"az":54,"ss":36,"used":true},{"PRN":15,"el":10,"az":307,"ss":40,"used":true},{"PRN":2,"el":31,"az":358,"ss":37,"used":true},{"PRN":28,"el":20,"az":89,"ss":35,"used":true},{"PRN":7,"el":10,"az":52,"ss":31,"used":false},{"PRN":19,"el":64,"az":148,"ss":45,"used":true},{"PRN":13,"el":10,"az":338,"ss":29,"used":false},{"PRN":15,"el":18,"az":297,"ss":33,"used":true},{"PRN":22,"el":10,"az":17,"ss":39,"used":false}]}]}
我有兴趣解析上面 JSON 中的卫星数组(下面提取)并返回两个数字:卫星数和使用的卫星数。例如:7 12
[
{
"PRN": 17,
"el": 42,
"az": 137,
"ss": 43,
"used": true
},
{
"PRN": 12,
"el": 27,
"az": 241,
"ss": 44,
"used": true
},
{
"PRN": 24,
"el": 53,
"az": 247,
"ss": 46,
"used": true
},
{
"PRN": 6,
"el": 55,
"az": 54,
"ss": 36,
"used": true
},
{
"PRN": 15,
"el": 10,
"az": 307,
"ss": 40,
"used": true
},
{
"PRN": 2,
"el": 31,
"az": 358,
"ss": 37,
"used": true
},
{
"PRN": 28,
"el": 20,
"az": 89,
"ss": 35,
"used": true
},
{
"PRN": 7,
"el": 10,
"az": 52,
"ss": 31,
"used": false
},
{
"PRN": 19,
"el": 64,
"az": 148,
"ss": 45,
"used": true
},
{
"PRN": 13,
"el": 10,
"az": 338,
"ss": 29,
"used": false
},
{
"PRN": 15,
"el": 18,
"az": 297,
"ss": 33,
"used": true
},
{
"PRN": 22,
"el": 10,
"az": 17,
"ss": 39,
"used": false
}
]
目前我只能弄清楚如何解析一个值,而不是一次解析两个值;这就是我所拥有的:
jq -r '[ .sky[] | .satellites[]| select(.used == true )| "\( .PRN )" ] |length'
输出使用的卫星数量,例如 7。
为了完整起见,如果它可能对某人有所帮助,我将按如下方式在 bash 中轮询 gpsd。此 returns 一个 JSON 结构,如 post 开头所示:
timeout 1 grep -m1 satellites < <(printf '%s\n' '?WATCH={"enable":true,"json":true};?POLL;' | nc 127.0.0.1 2947)
对于呈现的输入:
$ jq -r '.sky[].satellites | ( [ .[] | select(.used == true) ] | length ), length'
9
12
或者也许(但您也可以将其通过管道传输到 paste -sd" "
):
$ jq -r '.sky[].satellites | ( [ .[] | select(.used == true) ] | length | tostring) + " " + (length|tostring)'
9 12