Home > Back-end >  sed command is not working to find and replace content in yaml file?
sed command is not working to find and replace content in yaml file?

Time:11-02

I have following content in the yaml file des.yaml

simulator:
  enabled: false

i want to enable the simulator by making it true like:

simulator:
  enabled: true

by using sed command

I tried using sed command but its not working like:

sed -i 's|simulator\:\n  enabled\: false|simulator\:\n  enabled\:  true|' des.yaml

command is not throwing any error.

please help

CodePudding user response:

Using sed

$ sed -i '/simulator/ {N;s/\(enabled: \).*/\1true/}' input_file

If the match simulator is found, move to the next line, if the match enabled is present, the value will be changed to true.

Output

simulator:
  enabled: true

CodePudding user response:

Even though programs like sed can solve the problem, a proper tool should be used, here : https://github.com/mikefarah/yq/releases/

yq eval '.simulator.enabled=true' des.yaml

CodePudding user response:

This might work for you (GNU sed):

sed -i '/simulator:/{n;/enabled:/s/false/true/}' file

Match on simulator:, print the line and fetch the next.

Match on enabled:, replace false by true.

  • Related