Skip to main content
added 181 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

This answer assumes that you are using Mike Farah's yq utility (which you are, according to the error message you are showing). Andrey Kislyuk's yq is slightly different.


networkInterfaces is a list, so you can't access network or ipAddress beneath it without picking what list element to access these in.

If you only have the single element, you could use

yq '.networkInterfaces[0].network.ipAddress = "192.168.0.1"' file

That is, set the ipAddress of the network of the first element of the networkInterfaces list to the string 192.168.0.1.

To set the ipAddress of an entry with a particular id:

yq '.networkInterfaces[] |= select(.network.id == "network-1111").network.ipAddress = "192.168.0.1"' file

To use shell variables to hold the query id and the new IP number:

id=network-1111 newip=192.168.0.1 yq '.networkInterfaces[] |= select(.network.id == env(id)).network.ipAddress = env(newip)' file

networkInterfaces is a list, so you can't access network or ipAddress beneath it without picking what list element to access these in.

If you only have the single element, you could use

yq '.networkInterfaces[0].network.ipAddress = "192.168.0.1"' file

That is, set the ipAddress of the network of the first element of the networkInterfaces list to the string 192.168.0.1.

To set the ipAddress of an entry with a particular id:

yq '.networkInterfaces[] |= select(.network.id == "network-1111").network.ipAddress = "192.168.0.1"' file

To use shell variables to hold the query id and the new IP number:

id=network-1111 newip=192.168.0.1 yq '.networkInterfaces[] |= select(.network.id == env(id)).network.ipAddress = env(newip)' file

This answer assumes that you are using Mike Farah's yq utility (which you are, according to the error message you are showing). Andrey Kislyuk's yq is slightly different.


networkInterfaces is a list, so you can't access network or ipAddress beneath it without picking what list element to access these in.

If you only have the single element, you could use

yq '.networkInterfaces[0].network.ipAddress = "192.168.0.1"' file

That is, set the ipAddress of the network of the first element of the networkInterfaces list to the string 192.168.0.1.

To set the ipAddress of an entry with a particular id:

yq '.networkInterfaces[] |= select(.network.id == "network-1111").network.ipAddress = "192.168.0.1"' file

To use shell variables to hold the query id and the new IP number:

id=network-1111 newip=192.168.0.1 yq '.networkInterfaces[] |= select(.network.id == env(id)).network.ipAddress = env(newip)' file
added 181 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

networkInterfaces is a list, so you can't access network or ipAddress beneath it without picking what list element to access these in.

If you only have the single element, you could use

yq '.networkInterfaces[0].network.ipAddress = "192.168.0.1"' file

That is, set the ipAddress of the network of the first element of the networkInterfaces list to the string 192.168.0.1.

To set the ipAddress of an entry with a particular id:

yq '.networkInterfaces[] |= select(.network.id == "network-1111").network.ipAddress = "192.168.0.1"' file

To use shell variables to hold the query id and the new IP number:

id=network-1111 newip=192.168.0.1 yq '.networkInterfaces[] |= select(.network.id == env(id)).network.ipAddress = env(newip)' file

networkInterfaces is a list, so you can't access network or ipAddress beneath it without picking what list element to access these in.

If you only have the single element, you could use

yq '.networkInterfaces[0].network.ipAddress = "192.168.0.1"' file

That is, set the ipAddress of the network of the first element of the networkInterfaces list to the string 192.168.0.1.

networkInterfaces is a list, so you can't access network or ipAddress beneath it without picking what list element to access these in.

If you only have the single element, you could use

yq '.networkInterfaces[0].network.ipAddress = "192.168.0.1"' file

That is, set the ipAddress of the network of the first element of the networkInterfaces list to the string 192.168.0.1.

To set the ipAddress of an entry with a particular id:

yq '.networkInterfaces[] |= select(.network.id == "network-1111").network.ipAddress = "192.168.0.1"' file

To use shell variables to hold the query id and the new IP number:

id=network-1111 newip=192.168.0.1 yq '.networkInterfaces[] |= select(.network.id == env(id)).network.ipAddress = env(newip)' file
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

networkInterfaces is a list, so you can't access network or ipAddress beneath it without picking what list element to access these in.

If you only have the single element, you could use

yq '.networkInterfaces[0].network.ipAddress = "192.168.0.1"' file

That is, set the ipAddress of the network of the first element of the networkInterfaces list to the string 192.168.0.1.