Using sed to extract number from jsonHow to extract data from a JSON fileExtract UUID value from JSON...
Physical Interpretation of an Overdamped Pendulum
Quick destruction of a helium filled airship?
When does The Truman Show take place?
How do I pass a "list of lists" as the argument to a function of the form F[x,y]?
Why don't modern jet engines use forced exhaust mixing?
Why do so many people play out of turn on the last lead?
May the tower use the runway while an emergency aircraft is inbound?
Typesetting "hollow slash"
Unconventional examples of mathematical modelling
Why does "auf der Strecke bleiben" mean "to fall by the wayside"?
How to get locks that are keyed alike?
What would cause a nuclear power plant to break down after 2000 years, but not sooner?
Build a mob of suspiciously happy lenny faces ( ͡° ͜ʖ ͡°)
What allows us to use imaginary numbers?
Why do we use low resistance cables to minimize power losses?
Adding things to bunches of things vs multiplication
Insert or push_back to end of a std::vector?
How would armour (and combat) change if the fighter didn't need to actually wear it?
Did Michelle Obama have a staff of 23; and Melania have a staff of 4?
How does the Moon's gravity affect Earth's oceans despite Earth's stronger gravitational pull?
Is this bar slide trick shown on Cheers real or a visual effect?
QgsGeometry.length() giving wrong result?
Why does auto deduce this variable as double and not float?
100 Years of GCHQ - A quick afternoon puzzle!
Using sed to extract number from json
How to extract data from a JSON fileExtract UUID value from JSON returned from HTTP replyPrint just git commit sha and a pattern using sedRemove multiple regular expressions from variable with sedRetrieving values from json file using jqSupport with parsing JSON using sed neededExtract value from JSON document returned by curlextract data from JSON stringCreate a JSON file of all installed dpkg software
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I'm struggling to use a single sed command to extract a number from some json on Linux. Given the following block:
{
"key1": 100,
"key2": 200,
}
I'd like the output to be 100 in the above case, but I need to capture it regardless of its length.
So far I've got this:
sed -n '/key1/ s/.*: //p'
100,
I feel like I should be able to rid myself of the comma without piping out to 'tr' or whatever but I can't seem to manage it.
sed json
New contributor
add a comment |
I'm struggling to use a single sed command to extract a number from some json on Linux. Given the following block:
{
"key1": 100,
"key2": 200,
}
I'd like the output to be 100 in the above case, but I need to capture it regardless of its length.
So far I've got this:
sed -n '/key1/ s/.*: //p'
100,
I feel like I should be able to rid myself of the comma without piping out to 'tr' or whatever but I can't seem to manage it.
sed json
New contributor
7
With valid JSON:jq '.key1' file
– Cyrus
yesterday
7
JSON/XML/YAML/CSV should all be parsed with a dedicated parser that understands all the corner cases of the markup language.
– glenn jackman
yesterday
add a comment |
I'm struggling to use a single sed command to extract a number from some json on Linux. Given the following block:
{
"key1": 100,
"key2": 200,
}
I'd like the output to be 100 in the above case, but I need to capture it regardless of its length.
So far I've got this:
sed -n '/key1/ s/.*: //p'
100,
I feel like I should be able to rid myself of the comma without piping out to 'tr' or whatever but I can't seem to manage it.
sed json
New contributor
I'm struggling to use a single sed command to extract a number from some json on Linux. Given the following block:
{
"key1": 100,
"key2": 200,
}
I'd like the output to be 100 in the above case, but I need to capture it regardless of its length.
So far I've got this:
sed -n '/key1/ s/.*: //p'
100,
I feel like I should be able to rid myself of the comma without piping out to 'tr' or whatever but I can't seem to manage it.
sed json
sed json
New contributor
New contributor
edited yesterday
terdon♦
140k34 gold badges287 silver badges466 bronze badges
140k34 gold badges287 silver badges466 bronze badges
New contributor
asked yesterday
fatwashedfatwashed
42 bronze badges
42 bronze badges
New contributor
New contributor
7
With valid JSON:jq '.key1' file
– Cyrus
yesterday
7
JSON/XML/YAML/CSV should all be parsed with a dedicated parser that understands all the corner cases of the markup language.
– glenn jackman
yesterday
add a comment |
7
With valid JSON:jq '.key1' file
– Cyrus
yesterday
7
JSON/XML/YAML/CSV should all be parsed with a dedicated parser that understands all the corner cases of the markup language.
– glenn jackman
yesterday
7
7
With valid JSON:
jq '.key1' file
– Cyrus
yesterday
With valid JSON:
jq '.key1' file
– Cyrus
yesterday
7
7
JSON/XML/YAML/CSV should all be parsed with a dedicated parser that understands all the corner cases of the markup language.
– glenn jackman
yesterday
JSON/XML/YAML/CSV should all be parsed with a dedicated parser that understands all the corner cases of the markup language.
– glenn jackman
yesterday
add a comment |
2 Answers
2
active
oldest
votes
$ sed -e '/key1/!d' -e 's/.*: //' -e 's/,//' testfile
100
add a comment |
Just search for numbers:
$ sed -n '/key1/ s/.*: ([0-9][0-9]*).*/1/p' file
100
or
$ sed -En '/key1/ s/.*:s+([0-9]+).*/1/p' file
100
Personally, I would use grep
instead though:
$ grep -oP 'key1":s*Kd+' file
100
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
fatwashed is a new contributor. Be nice, and check out our Code of Conduct.
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%2funix.stackexchange.com%2fquestions%2f535728%2fusing-sed-to-extract-number-from-json%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
$ sed -e '/key1/!d' -e 's/.*: //' -e 's/,//' testfile
100
add a comment |
$ sed -e '/key1/!d' -e 's/.*: //' -e 's/,//' testfile
100
add a comment |
$ sed -e '/key1/!d' -e 's/.*: //' -e 's/,//' testfile
100
$ sed -e '/key1/!d' -e 's/.*: //' -e 's/,//' testfile
100
answered yesterday
markgrafmarkgraf
1857 bronze badges
1857 bronze badges
add a comment |
add a comment |
Just search for numbers:
$ sed -n '/key1/ s/.*: ([0-9][0-9]*).*/1/p' file
100
or
$ sed -En '/key1/ s/.*:s+([0-9]+).*/1/p' file
100
Personally, I would use grep
instead though:
$ grep -oP 'key1":s*Kd+' file
100
add a comment |
Just search for numbers:
$ sed -n '/key1/ s/.*: ([0-9][0-9]*).*/1/p' file
100
or
$ sed -En '/key1/ s/.*:s+([0-9]+).*/1/p' file
100
Personally, I would use grep
instead though:
$ grep -oP 'key1":s*Kd+' file
100
add a comment |
Just search for numbers:
$ sed -n '/key1/ s/.*: ([0-9][0-9]*).*/1/p' file
100
or
$ sed -En '/key1/ s/.*:s+([0-9]+).*/1/p' file
100
Personally, I would use grep
instead though:
$ grep -oP 'key1":s*Kd+' file
100
Just search for numbers:
$ sed -n '/key1/ s/.*: ([0-9][0-9]*).*/1/p' file
100
or
$ sed -En '/key1/ s/.*:s+([0-9]+).*/1/p' file
100
Personally, I would use grep
instead though:
$ grep -oP 'key1":s*Kd+' file
100
answered yesterday
terdon♦terdon
140k34 gold badges287 silver badges466 bronze badges
140k34 gold badges287 silver badges466 bronze badges
add a comment |
add a comment |
fatwashed is a new contributor. Be nice, and check out our Code of Conduct.
fatwashed is a new contributor. Be nice, and check out our Code of Conduct.
fatwashed is a new contributor. Be nice, and check out our Code of Conduct.
fatwashed is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f535728%2fusing-sed-to-extract-number-from-json%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
7
With valid JSON:
jq '.key1' file
– Cyrus
yesterday
7
JSON/XML/YAML/CSV should all be parsed with a dedicated parser that understands all the corner cases of the markup language.
– glenn jackman
yesterday