Grep value of a specific key from a String, concatenated of key : value pairsReversing the value key pairs of array using sed or pattern replacement or brace expansion?extracting specific substrings from stringReading a string till a key word and replacing from there with another stringMatching “keyword value” pairs from semi-structured inputComplete key value pairssed/awk replace a specific pattern under another patternChecking duplicate value in specific column in csv file with pipe seperatedGrep a list of words from pairs of filesHow to grep lines which have more than specific number of special charactersremove specific characters from a string

Is it a good idea to teach algorithm courses using pseudocode instead of a real programming language?

How do we explain the use of a software on a math paper?

pwaS eht tirsf dna tasl setterl fo hace dorw

Addressing an email

What were the "pills" that were added to solid waste in Apollo 7?

Why is so much ransomware breakable?

Latin words remembered from high school 50 years ago

Why did Nick Fury not hesitate in blowing up the plane he thought was carrying a nuke?

Good examples of "two is easy, three is hard" in computational sciences

How to fix "webpack Dev Server Invalid Options" in Vuejs

Would it be possible to set up a franchise in the ancient world?

Print characters from list with a For-loop

Can 2 light bulbs of 120V in series be used on 230V AC?

Precedent for disabled Kings

Why does Taylor’s series “work”?

Greek theta instead of lower case þ (Icelandic) in TexStudio

Why would Thor need to strike a building with lightning to attack enemies?

Can a Warforged have a ranged weapon affixed to them like an armblade?

Is it possible to view all the attribute data in QGIS

How could Dwarves prevent sand from filling up their settlements

Have I found a major security issue with login

Isn't Kirchhoff's junction law a violation of conservation of charge?

How to plot a surface from a system of equations?

What halachos of mourning apply to a grandchild for his grandparent's death?



Grep value of a specific key from a String, concatenated of key : value pairs


Reversing the value key pairs of array using sed or pattern replacement or brace expansion?extracting specific substrings from stringReading a string till a key word and replacing from there with another stringMatching “keyword value” pairs from semi-structured inputComplete key value pairssed/awk replace a specific pattern under another patternChecking duplicate value in specific column in csv file with pipe seperatedGrep a list of words from pairs of filesHow to grep lines which have more than specific number of special charactersremove specific characters from a string






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I have a string which is concatenation of "key":"value" pairs seperated by "," like-



KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3


And from this string I have to grep value for a specific string,lets say KEY2, so output of our command should be VALUE2.










share|improve this question







New contributor



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














  • 3





    Can the values contain quotes commas or colons? Is it JSON?

    – Jeff Schaller
    May 13 at 12:54

















2















I have a string which is concatenation of "key":"value" pairs seperated by "," like-



KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3


And from this string I have to grep value for a specific string,lets say KEY2, so output of our command should be VALUE2.










share|improve this question







New contributor



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














  • 3





    Can the values contain quotes commas or colons? Is it JSON?

    – Jeff Schaller
    May 13 at 12:54













2












2








2








I have a string which is concatenation of "key":"value" pairs seperated by "," like-



KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3


And from this string I have to grep value for a specific string,lets say KEY2, so output of our command should be VALUE2.










share|improve this question







New contributor



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











I have a string which is concatenation of "key":"value" pairs seperated by "," like-



KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3


And from this string I have to grep value for a specific string,lets say KEY2, so output of our command should be VALUE2.







shell-script awk sed






share|improve this question







New contributor



Abhijeet srivastava 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



Abhijeet srivastava 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






New contributor



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








asked May 13 at 12:49









Abhijeet srivastavaAbhijeet srivastava

111




111




New contributor



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




New contributor




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









  • 3





    Can the values contain quotes commas or colons? Is it JSON?

    – Jeff Schaller
    May 13 at 12:54












  • 3





    Can the values contain quotes commas or colons? Is it JSON?

    – Jeff Schaller
    May 13 at 12:54







3




3





Can the values contain quotes commas or colons? Is it JSON?

– Jeff Schaller
May 13 at 12:54





Can the values contain quotes commas or colons? Is it JSON?

– Jeff Schaller
May 13 at 12:54










3 Answers
3






active

oldest

votes


















8














Using pgrep:



grep -Po '(^|[ ,])KEY1:K[^,]*'


or egrep and cut:



grep -Eo '(^|[ ,])KEY2:[^,]*' | cut -d: -f2-


For both methods, the Value is not allowed to contain comma.




If you had proper json, e.g.



 "KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3" 


you could use jq:



jq ".KEY2"





share|improve this answer
































    0














    With regular grep assuming VALUE doesn't contain a colon:



    grep -o 'KEY2:[^,]+' | grep -o '[^:]+$'





    share|improve this answer

























    • @RoVo: added caveat

      – Thor
      May 13 at 14:25


















    0














    To grep only the value



    echo $myString | grep -Po "(?<=KEY2:)[^,]*"


    or



    grep -Po "(?<=KEY2:)[^,]*" <<< $myString





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



      );






      Abhijeet srivastava 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%2f518673%2fgrep-value-of-a-specific-key-from-a-string-concatenated-of-key-value-pairs%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      8














      Using pgrep:



      grep -Po '(^|[ ,])KEY1:K[^,]*'


      or egrep and cut:



      grep -Eo '(^|[ ,])KEY2:[^,]*' | cut -d: -f2-


      For both methods, the Value is not allowed to contain comma.




      If you had proper json, e.g.



       "KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3" 


      you could use jq:



      jq ".KEY2"





      share|improve this answer





























        8














        Using pgrep:



        grep -Po '(^|[ ,])KEY1:K[^,]*'


        or egrep and cut:



        grep -Eo '(^|[ ,])KEY2:[^,]*' | cut -d: -f2-


        For both methods, the Value is not allowed to contain comma.




        If you had proper json, e.g.



         "KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3" 


        you could use jq:



        jq ".KEY2"





        share|improve this answer



























          8












          8








          8







          Using pgrep:



          grep -Po '(^|[ ,])KEY1:K[^,]*'


          or egrep and cut:



          grep -Eo '(^|[ ,])KEY2:[^,]*' | cut -d: -f2-


          For both methods, the Value is not allowed to contain comma.




          If you had proper json, e.g.



           "KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3" 


          you could use jq:



          jq ".KEY2"





          share|improve this answer















          Using pgrep:



          grep -Po '(^|[ ,])KEY1:K[^,]*'


          or egrep and cut:



          grep -Eo '(^|[ ,])KEY2:[^,]*' | cut -d: -f2-


          For both methods, the Value is not allowed to contain comma.




          If you had proper json, e.g.



           "KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3" 


          you could use jq:



          jq ".KEY2"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 14 at 9:02

























          answered May 13 at 12:56









          pLumopLumo

          5,450925




          5,450925























              0














              With regular grep assuming VALUE doesn't contain a colon:



              grep -o 'KEY2:[^,]+' | grep -o '[^:]+$'





              share|improve this answer

























              • @RoVo: added caveat

                – Thor
                May 13 at 14:25















              0














              With regular grep assuming VALUE doesn't contain a colon:



              grep -o 'KEY2:[^,]+' | grep -o '[^:]+$'





              share|improve this answer

























              • @RoVo: added caveat

                – Thor
                May 13 at 14:25













              0












              0








              0







              With regular grep assuming VALUE doesn't contain a colon:



              grep -o 'KEY2:[^,]+' | grep -o '[^:]+$'





              share|improve this answer















              With regular grep assuming VALUE doesn't contain a colon:



              grep -o 'KEY2:[^,]+' | grep -o '[^:]+$'






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited May 13 at 14:24

























              answered May 13 at 14:18









              ThorThor

              12.4k13963




              12.4k13963












              • @RoVo: added caveat

                – Thor
                May 13 at 14:25

















              • @RoVo: added caveat

                – Thor
                May 13 at 14:25
















              @RoVo: added caveat

              – Thor
              May 13 at 14:25





              @RoVo: added caveat

              – Thor
              May 13 at 14:25











              0














              To grep only the value



              echo $myString | grep -Po "(?<=KEY2:)[^,]*"


              or



              grep -Po "(?<=KEY2:)[^,]*" <<< $myString





              share|improve this answer



























                0














                To grep only the value



                echo $myString | grep -Po "(?<=KEY2:)[^,]*"


                or



                grep -Po "(?<=KEY2:)[^,]*" <<< $myString





                share|improve this answer

























                  0












                  0








                  0







                  To grep only the value



                  echo $myString | grep -Po "(?<=KEY2:)[^,]*"


                  or



                  grep -Po "(?<=KEY2:)[^,]*" <<< $myString





                  share|improve this answer













                  To grep only the value



                  echo $myString | grep -Po "(?<=KEY2:)[^,]*"


                  or



                  grep -Po "(?<=KEY2:)[^,]*" <<< $myString






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 13 at 14:52









                  bu5hmanbu5hman

                  1,388415




                  1,388415




















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









                      draft saved

                      draft discarded


















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












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











                      Abhijeet srivastava 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%2f518673%2fgrep-value-of-a-specific-key-from-a-string-concatenated-of-key-value-pairs%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

                      Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

                      Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

                      Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림