githubEdit

Editing files in place

With V3, built-in file editing was removed to allow for re-evaluation. In the meantime, you can edit files in place using shell redirection and a temporary file.


Basic pattern

dasel -i <format> --root '<query>' < file > file.tmp && mv file.tmp file
  1. Read the file into dasel via < file

  2. Redirect the output to a temporary file via > file.tmp

  3. Move the temporary file over the original via mv file.tmp file

It is important to use the --root flag — this ensures dasel outputs the entire document, not just the modified value.


Examples

Update a value in a TOML file

dasel -i toml --root 'foo = "bar"' < config.toml > config.toml.tmp \
  && mv config.toml.tmp config.toml

Update a value in a JSON file

dasel -i json --root 'settings.theme = "dark"' < config.json > config.json.tmp \
  && mv config.json.tmp config.json

Add a new field to a YAML file

Shell function for convenience

You can wrap this pattern in a shell function:


Why use a temporary file?

When you redirect output to a file, the shell truncates that file before running the command. This means the file would be empty when dasel tries to read it. The temporary file avoids this race condition.

Last updated