1

I have the following json file :

{
  "src": "ssh://git@bitbucket.org/myrepos/project",
  "version": "master",
  "archetype_version": "master",
  "roles": [
    {
      "src": "ssh://git@bitbucket.org/myrepos/project",
      "version": "master"
    }
  ]
}

I want to change the version from master to another one, so I do the following command:

 cat file.json | jq '.roles[0].version = "new"' | jq '.version = "new"' | jq '.archetype_version = "new"' > file2.json

How to save the changes to file.json not to file2.json because I'll need to use the same file later?

peak
  • 105,803
  • 17
  • 152
  • 177
Souad
  • 4,856
  • 15
  • 80
  • 140

2 Answers2

1

You could also use sponge in "moreutils" (https://joeyh.name/code/moreutils/). See the jq FAQ for further details and options.

By the way, you can compress your pipeline so that there is just one invocation of jq and no invocation of cat:

< file.json jq '
.roles[0].version = "new"
| .version = "new"
| .archetype_version = "new"' | sponge file.json

Of course overwriting the input file without first backing it up generally carries some risk, so in many ways a two-step process such as the following is best:

mv file.json file.tmp && < file.tmp jq .... > file.json
peak
  • 105,803
  • 17
  • 152
  • 177
0

one way is:

..file1.json|..|..|.. >file2.json && mv file2.json file1.json
Kent
  • 189,393
  • 32
  • 233
  • 301