简体   繁体   中英

Insert element using github.com/mikefarah/yq command in a file

I have below content in yml file:

category:
  toolSettings: settings.xml

Below snippet needs to be added to the existing under category:

 env:                
    variables:
       - user: ABC
       - passowrd: BCD

Expected Output:

category:
  env:                
    variables:
       - user: ABC
       - passowrd: BCD
  toolSettings: settings.xml

Tried below:

yq e '."category" +=({env: {variables:[ {"user":"ABC"},{"passowrd":"BCD"}]}})'  jules.yml > tmp.yml
yq -i '.category.env.variables[0].user="ABC"' jules.yml > tmp.yml
yq -i '.category.env.variables[1].passowrd="BCD"' jules.yml > tmp.yml

But none of the above are not working.

github.com/mikefarah/yq, 
yq Version: 4.26.1

One should use either yq -i or a redirection on stdout, not both together.


The following code:

# Create original file
cat >jules.yml <<'EOF'
category:
  toolSettings: settings.xml
EOF

# Edit that file in-place
yq -i '
  .category.env.variables.user = "ABC"
| .category.env.variables.password = "DEF"
' jules.yml

# Write file to output
echo "New jules.yml file follows:"
echo "---"
cat jules.yml

...leaves jules.yml with the content:

category:
  toolSettings: settings.xml
  env:
    variables:
      user: ABC
      password: DEF

...as you can see at https://replit.com/@CharlesDuffy2/EnragedPreviousScans#runme.bash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM