I have the following list:
{
"list": [
{
"ipv4": [
"192.168.64.193"
],
"name": "node2",
"release": "20.04 LTS",
"state": "Running"
},
{
"ipv4": [
"192.168.64.192"
],
"name": "node1",
"release": "20.04 LTS",
"state": "Running"
}
]
}
jq -r '.list[] | [ .name, .ipv4[0] ] | @tsv' file.json
node2 192.168.64.193
node1 192.168.64.192
How can I sort the list?
I am trying to sort by .name:
jq -r 'sort_by(.name) | .list[] | [ .name, .ipv4[0] ] | @tsv' file.json
Cannot index array with string "name"
jq -r 'sort_by(.list[].name) | .list[] | [ .name, .ipv4[0] ] | @tsv' file.json
Cannot index array with string "name"
CodePudding user response:
Insert sort_by(.name)
after traversing to .list
but before iterating over its items with []
:
jq -r '.list | sort_by(.name)[] | [.name, .ipv4[0]] | @tsv' file.json
node1 192.168.64.192
node2 192.168.64.193