Differences

This shows you the differences between two versions of the page.

Link to this comparison view

shell [2018/12/14 10:30]
dblume Added jq
shell [2019/01/17 21:25] (current)
dblume [jq Tips]
Line 495: Line 495:
  
 <code> <code>
-cat fruits.txt | jq '.fruits|del(.count,.open)|with_entries(.value |= .price)'+$ jq '.fruits|del(.count,.open)|with_entries(.value |= .price)' fruits.txt
 { {
   "apple": 2,   "apple": 2,
   "banana": 3   "banana": 3
 } }
 +
 +# with_entries(f) is an alias for to_entries | map(x) | from_entries
 +
 +jq '.fruits|del(.count,.open)|to_entries|map(.value |= .price)|from_entries' f
 +{
 +  "apple": 2,
 +  "banana": 3
 +}
 +
 +$ jq '.fruits|del(.count,.open)|[to_entries[]|{(.key): .value.price}]|add' fruits.txt
 +{
 +  "apple": 2,
 +  "banana": 3
 +}
 +</code>
 +
 +<file javascript fruit_ip.txt>
 +{
 +  "192.168.144.52": {
 +    "ipAddress": "192.168.144.52",
 +    "attributes": {
 +      "model": "apple",
 +      "name": "David's apple"
 +    }
 +  },
 +  "192.168.144.40": {
 +    "ipAddress": "192.168.144.40",
 +    "attributes": {
 +      "model": "banana",
 +      "name": "David's banana"
 +    }
 +  }
 +}
 +</file>
 +
 +<code>
 +$ jq '[to_entries[]|{"key":.value.attributes.name,"value":.key}]|from_entries' fruit_ip.txt
 +{
 +  "David's apple": "192.168.144.52",
 +  "David's banana": "192.168.144.40"
 +}
 +
 +$ jq '[.[]|{(.attributes.name):.ipAddress}]|add' fruit_ip.txt
 +{
 +  "David's apple": "192.168.144.52",
 +  "David's banana": "192.168.144.40"
 +}
 +
 +$ jq -r "to_entries|map(\"\(.value.attributes.name) = \(.key)\")|.[]" fruit_ip.txt
 +David's apple = 192.168.144.52
 +David's banana = 192.168.144.40
 </code> </code>
 +
 +
 ====== Additional Keywords ====== ====== Additional Keywords ======
  
 Linux, Unix, *nix Linux, Unix, *nix
shell.1544812211.txt.gz · Last modified: 2018/12/14 10:30 by dblume
 
Recent changes RSS feed Driven by DokuWiki