Increment value and save back to the file
I have a simple JSON file like this,
{
...
"version": "2.1.0",
"buildNumber": 83
}
I want to simply increment the buildNumber by one in the file. Rest of the content should remain as is.
What I have come up with so far is bellow.
jq -e '.buildNumber + 1' 'package.json' > 'package.json.tmp' && cp 'package.json.tmp' 'package.json'
But after running the script I'm only left with the updated value in the package.json file, which is just 84. The rest of the content is gone. How do I fix this?
json increment jq
add a comment |
I have a simple JSON file like this,
{
...
"version": "2.1.0",
"buildNumber": 83
}
I want to simply increment the buildNumber by one in the file. Rest of the content should remain as is.
What I have come up with so far is bellow.
jq -e '.buildNumber + 1' 'package.json' > 'package.json.tmp' && cp 'package.json.tmp' 'package.json'
But after running the script I'm only left with the updated value in the package.json file, which is just 84. The rest of the content is gone. How do I fix this?
json increment jq
add a comment |
I have a simple JSON file like this,
{
...
"version": "2.1.0",
"buildNumber": 83
}
I want to simply increment the buildNumber by one in the file. Rest of the content should remain as is.
What I have come up with so far is bellow.
jq -e '.buildNumber + 1' 'package.json' > 'package.json.tmp' && cp 'package.json.tmp' 'package.json'
But after running the script I'm only left with the updated value in the package.json file, which is just 84. The rest of the content is gone. How do I fix this?
json increment jq
I have a simple JSON file like this,
{
...
"version": "2.1.0",
"buildNumber": 83
}
I want to simply increment the buildNumber by one in the file. Rest of the content should remain as is.
What I have come up with so far is bellow.
jq -e '.buildNumber + 1' 'package.json' > 'package.json.tmp' && cp 'package.json.tmp' 'package.json'
But after running the script I'm only left with the updated value in the package.json file, which is just 84. The rest of the content is gone. How do I fix this?
json increment jq
json increment jq
edited Nov 27 at 4:13
peak
30.2k83956
30.2k83956
asked Nov 21 at 7:07
JanithaR
6491917
6491917
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
First, the filter to use is:
.buildNumber += 1
Second, as you evidently realize, it would be unwise to use > to overwrite the file. One option to consider if it’s admissible would be to use coreutil’s sponge
. Another would be to rename the input file first.
I just found out about jq a couple of hours ago and I'm not quite familiar with bash every since I learned the basics of it like 8 years ago. I was under the impression that by doingjq '.buildNumber + 1' 'package.json'
the full contents of the file would be in memory including the modification. And so I was simply writing that content to a temp file and the evidently replacing the original file with the temp file contents. Am I understanding how jq works wrong?
– JanithaR
Nov 21 at 7:24
uYes, that is incorrect. As you can easily check, .buildnumber + 1 adds 1 to .buildnumber, yielding an integer.
– peak
Nov 21 at 7:28
So what I'm passing on to the package.json.temp file is simply that value, which in this case just 84. So if I usecurl
and read the full content of the file and then dojq '.buildNumber += 1' 'package.json'
and then carry on as is, that would give me the ouput I'm expecting yes?
– JanithaR
Nov 21 at 7:38
add a comment |
I just put the two commands as bellow in two lines and it worked just as I wanted.
jq '.buildNumber += 1' 'package.json' > 'package.json.tmp'
cp 'package.json.tmp' 'package.json'
A safer variation would be along the lines of:mv package.json package.json.bak && jq ... < package.json.bak > package.json
; that way, the original file contents should not be lost under any circumstances.
– peak
Nov 27 at 4:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406879%2fincrement-value-and-save-back-to-the-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
First, the filter to use is:
.buildNumber += 1
Second, as you evidently realize, it would be unwise to use > to overwrite the file. One option to consider if it’s admissible would be to use coreutil’s sponge
. Another would be to rename the input file first.
I just found out about jq a couple of hours ago and I'm not quite familiar with bash every since I learned the basics of it like 8 years ago. I was under the impression that by doingjq '.buildNumber + 1' 'package.json'
the full contents of the file would be in memory including the modification. And so I was simply writing that content to a temp file and the evidently replacing the original file with the temp file contents. Am I understanding how jq works wrong?
– JanithaR
Nov 21 at 7:24
uYes, that is incorrect. As you can easily check, .buildnumber + 1 adds 1 to .buildnumber, yielding an integer.
– peak
Nov 21 at 7:28
So what I'm passing on to the package.json.temp file is simply that value, which in this case just 84. So if I usecurl
and read the full content of the file and then dojq '.buildNumber += 1' 'package.json'
and then carry on as is, that would give me the ouput I'm expecting yes?
– JanithaR
Nov 21 at 7:38
add a comment |
First, the filter to use is:
.buildNumber += 1
Second, as you evidently realize, it would be unwise to use > to overwrite the file. One option to consider if it’s admissible would be to use coreutil’s sponge
. Another would be to rename the input file first.
I just found out about jq a couple of hours ago and I'm not quite familiar with bash every since I learned the basics of it like 8 years ago. I was under the impression that by doingjq '.buildNumber + 1' 'package.json'
the full contents of the file would be in memory including the modification. And so I was simply writing that content to a temp file and the evidently replacing the original file with the temp file contents. Am I understanding how jq works wrong?
– JanithaR
Nov 21 at 7:24
uYes, that is incorrect. As you can easily check, .buildnumber + 1 adds 1 to .buildnumber, yielding an integer.
– peak
Nov 21 at 7:28
So what I'm passing on to the package.json.temp file is simply that value, which in this case just 84. So if I usecurl
and read the full content of the file and then dojq '.buildNumber += 1' 'package.json'
and then carry on as is, that would give me the ouput I'm expecting yes?
– JanithaR
Nov 21 at 7:38
add a comment |
First, the filter to use is:
.buildNumber += 1
Second, as you evidently realize, it would be unwise to use > to overwrite the file. One option to consider if it’s admissible would be to use coreutil’s sponge
. Another would be to rename the input file first.
First, the filter to use is:
.buildNumber += 1
Second, as you evidently realize, it would be unwise to use > to overwrite the file. One option to consider if it’s admissible would be to use coreutil’s sponge
. Another would be to rename the input file first.
answered Nov 21 at 7:14
peak
30.2k83956
30.2k83956
I just found out about jq a couple of hours ago and I'm not quite familiar with bash every since I learned the basics of it like 8 years ago. I was under the impression that by doingjq '.buildNumber + 1' 'package.json'
the full contents of the file would be in memory including the modification. And so I was simply writing that content to a temp file and the evidently replacing the original file with the temp file contents. Am I understanding how jq works wrong?
– JanithaR
Nov 21 at 7:24
uYes, that is incorrect. As you can easily check, .buildnumber + 1 adds 1 to .buildnumber, yielding an integer.
– peak
Nov 21 at 7:28
So what I'm passing on to the package.json.temp file is simply that value, which in this case just 84. So if I usecurl
and read the full content of the file and then dojq '.buildNumber += 1' 'package.json'
and then carry on as is, that would give me the ouput I'm expecting yes?
– JanithaR
Nov 21 at 7:38
add a comment |
I just found out about jq a couple of hours ago and I'm not quite familiar with bash every since I learned the basics of it like 8 years ago. I was under the impression that by doingjq '.buildNumber + 1' 'package.json'
the full contents of the file would be in memory including the modification. And so I was simply writing that content to a temp file and the evidently replacing the original file with the temp file contents. Am I understanding how jq works wrong?
– JanithaR
Nov 21 at 7:24
uYes, that is incorrect. As you can easily check, .buildnumber + 1 adds 1 to .buildnumber, yielding an integer.
– peak
Nov 21 at 7:28
So what I'm passing on to the package.json.temp file is simply that value, which in this case just 84. So if I usecurl
and read the full content of the file and then dojq '.buildNumber += 1' 'package.json'
and then carry on as is, that would give me the ouput I'm expecting yes?
– JanithaR
Nov 21 at 7:38
I just found out about jq a couple of hours ago and I'm not quite familiar with bash every since I learned the basics of it like 8 years ago. I was under the impression that by doing
jq '.buildNumber + 1' 'package.json'
the full contents of the file would be in memory including the modification. And so I was simply writing that content to a temp file and the evidently replacing the original file with the temp file contents. Am I understanding how jq works wrong?– JanithaR
Nov 21 at 7:24
I just found out about jq a couple of hours ago and I'm not quite familiar with bash every since I learned the basics of it like 8 years ago. I was under the impression that by doing
jq '.buildNumber + 1' 'package.json'
the full contents of the file would be in memory including the modification. And so I was simply writing that content to a temp file and the evidently replacing the original file with the temp file contents. Am I understanding how jq works wrong?– JanithaR
Nov 21 at 7:24
uYes, that is incorrect. As you can easily check, .buildnumber + 1 adds 1 to .buildnumber, yielding an integer.
– peak
Nov 21 at 7:28
uYes, that is incorrect. As you can easily check, .buildnumber + 1 adds 1 to .buildnumber, yielding an integer.
– peak
Nov 21 at 7:28
So what I'm passing on to the package.json.temp file is simply that value, which in this case just 84. So if I use
curl
and read the full content of the file and then do jq '.buildNumber += 1' 'package.json'
and then carry on as is, that would give me the ouput I'm expecting yes?– JanithaR
Nov 21 at 7:38
So what I'm passing on to the package.json.temp file is simply that value, which in this case just 84. So if I use
curl
and read the full content of the file and then do jq '.buildNumber += 1' 'package.json'
and then carry on as is, that would give me the ouput I'm expecting yes?– JanithaR
Nov 21 at 7:38
add a comment |
I just put the two commands as bellow in two lines and it worked just as I wanted.
jq '.buildNumber += 1' 'package.json' > 'package.json.tmp'
cp 'package.json.tmp' 'package.json'
A safer variation would be along the lines of:mv package.json package.json.bak && jq ... < package.json.bak > package.json
; that way, the original file contents should not be lost under any circumstances.
– peak
Nov 27 at 4:18
add a comment |
I just put the two commands as bellow in two lines and it worked just as I wanted.
jq '.buildNumber += 1' 'package.json' > 'package.json.tmp'
cp 'package.json.tmp' 'package.json'
A safer variation would be along the lines of:mv package.json package.json.bak && jq ... < package.json.bak > package.json
; that way, the original file contents should not be lost under any circumstances.
– peak
Nov 27 at 4:18
add a comment |
I just put the two commands as bellow in two lines and it worked just as I wanted.
jq '.buildNumber += 1' 'package.json' > 'package.json.tmp'
cp 'package.json.tmp' 'package.json'
I just put the two commands as bellow in two lines and it worked just as I wanted.
jq '.buildNumber += 1' 'package.json' > 'package.json.tmp'
cp 'package.json.tmp' 'package.json'
answered Nov 22 at 14:57
JanithaR
6491917
6491917
A safer variation would be along the lines of:mv package.json package.json.bak && jq ... < package.json.bak > package.json
; that way, the original file contents should not be lost under any circumstances.
– peak
Nov 27 at 4:18
add a comment |
A safer variation would be along the lines of:mv package.json package.json.bak && jq ... < package.json.bak > package.json
; that way, the original file contents should not be lost under any circumstances.
– peak
Nov 27 at 4:18
A safer variation would be along the lines of:
mv package.json package.json.bak && jq ... < package.json.bak > package.json
; that way, the original file contents should not be lost under any circumstances.– peak
Nov 27 at 4:18
A safer variation would be along the lines of:
mv package.json package.json.bak && jq ... < package.json.bak > package.json
; that way, the original file contents should not be lost under any circumstances.– peak
Nov 27 at 4:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406879%2fincrement-value-and-save-back-to-the-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown