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;
}







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.










share|improve this question









New contributor



fatwashed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • 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


















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.










share|improve this question









New contributor



fatwashed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • 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














0












0








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.










share|improve this question









New contributor



fatwashed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor



fatwashed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



fatwashed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited yesterday









terdon

140k34 gold badges287 silver badges466 bronze badges




140k34 gold badges287 silver badges466 bronze badges






New contributor



fatwashed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked yesterday









fatwashedfatwashed

42 bronze badges




42 bronze badges




New contributor



fatwashed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




fatwashed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • 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





    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










2 Answers
2






active

oldest

votes


















2














$ sed -e '/key1/!d' -e 's/.*: //' -e 's/,//' testfile 
100





share|improve this answer

































    1














    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





    share|improve this answer




























      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.










      draft saved

      draft discarded


















      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









      2














      $ sed -e '/key1/!d' -e 's/.*: //' -e 's/,//' testfile 
      100





      share|improve this answer






























        2














        $ sed -e '/key1/!d' -e 's/.*: //' -e 's/,//' testfile 
        100





        share|improve this answer




























          2












          2








          2







          $ sed -e '/key1/!d' -e 's/.*: //' -e 's/,//' testfile 
          100





          share|improve this answer













          $ sed -e '/key1/!d' -e 's/.*: //' -e 's/,//' testfile 
          100






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          markgrafmarkgraf

          1857 bronze badges




          1857 bronze badges




























              1














              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





              share|improve this answer






























                1














                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





                share|improve this answer




























                  1












                  1








                  1







                  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





                  share|improve this answer













                  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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered yesterday









                  terdonterdon

                  140k34 gold badges287 silver badges466 bronze badges




                  140k34 gold badges287 silver badges466 bronze badges

























                      fatwashed is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                      Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                      Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...