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?Reading 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 patternHow to grep two strings in line by specific order AND calculating values line by line according to my grepHow to grep lines which have more than specific number of special charactersHow to select specific columns from a file with a string separator

Car as a good investment

Latest newtx package update (v1.601 Oct 2, 2019) breaks the footnote command [update: bug fixed by package author]

How many wires can safely be secured in a Marrette 33 wire nut?

Code Golf Measurer © 2019

Did Terry Pratchett ever explain the inspiration behind the Luggage?

Get injured / Get increased

Why do these two ways of understanding constant acceleration give different results?

How can you tell apart the pronounciation at the end between the "meine" and "meiner" in the daily spoken situation?

How are Aircraft Noses Designed?

How to extract *.tgz.part-*?

Is oxygen above the critical point always supercritical fluid? Would it still appear to roughly follow the ideal gas law?

Generating sequential alphanumeric values that match a certain pattern

How (and if) to include name change for transgender person in genealogy?

How to deal with people whose priority is to not get blamed?

What are the differences between prismatic, lensatic, and optical sighting compasses?

Do more Americans want the Bidens investigated than Trump impeached?

Can we not simply connect a battery to a RAM to prevent data loss during power cuts?

How do I remove 'None' items from the end of a list in Python

Can a species eat water?

What fantasy book has twins (except one's blue) and a cloaked ice bear on the cover?

Does immunity to fear prevent a mummy's Dreadful Glare from paralyzing a character?

Does Darwin owe a debt to Hegel?

Which culture used no personal names?

Idiom for a situation or event that makes one poor or even poorer?



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?Reading 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 patternHow to grep two strings in line by specific order AND calculating values line by line according to my grepHow to grep lines which have more than specific number of special charactersHow to select specific columns from a file with a string separator






.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 separated by "," like-



KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3


From this string, I have to grep for a specific string -- let's say KEY2 -- so the output of our command should be VALUE2.










share|improve this question
























  • 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 separated by "," like-



KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3


From this string, I have to grep for a specific string -- let's say KEY2 -- so the output of our command should be VALUE2.










share|improve this question
























  • 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 separated by "," like-



KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3


From this string, I have to grep for a specific string -- let's say KEY2 -- so the output of our command should be VALUE2.










share|improve this question

















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



KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3


From this string, I have to grep for a specific string -- let's say KEY2 -- so the output of our command should be VALUE2.







shell-script awk sed






share|improve this question
















share|improve this question













share|improve this question




share|improve this question








edited May 19 at 10:43









Jeff Schaller

50.3k11 gold badges74 silver badges167 bronze badges




50.3k11 gold badges74 silver badges167 bronze badges










asked May 13 at 12:49









Abhijeet srivastavaAbhijeet srivastava

111 bronze badge




111 bronze badge










  • 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










4 Answers
4






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

































      0


















      Use awk, read key:value pair as record, read key, value as 1st and 2nd field.



      awk -v RS=' *, *' -v FS=' *: *' '$1=="KEY2"print $2' <<<$str



      • -v RS=' *, *' set record separator to , and it's surrounding space


      • -v FS=' *: *' set field separator to : and it's surrounding space


      • '$1=="KEY2"print $2' print value if key found. change "KEY2" to your desired key value.





      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/4.0/"u003ecc by-sa 4.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
        );



        );














        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


























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        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

            7,93115 silver badges36 bronze badges




            7,93115 silver badges36 bronze badges


























                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

                13.1k1 gold badge41 silver badges64 bronze badges




                13.1k1 gold badge41 silver badges64 bronze badges















                • @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,7331 gold badge4 silver badges15 bronze badges




                    1,7331 gold badge4 silver badges15 bronze badges
























                        0


















                        Use awk, read key:value pair as record, read key, value as 1st and 2nd field.



                        awk -v RS=' *, *' -v FS=' *: *' '$1=="KEY2"print $2' <<<$str



                        • -v RS=' *, *' set record separator to , and it's surrounding space


                        • -v FS=' *: *' set field separator to : and it's surrounding space


                        • '$1=="KEY2"print $2' print value if key found. change "KEY2" to your desired key value.





                        share|improve this answer






























                          0


















                          Use awk, read key:value pair as record, read key, value as 1st and 2nd field.



                          awk -v RS=' *, *' -v FS=' *: *' '$1=="KEY2"print $2' <<<$str



                          • -v RS=' *, *' set record separator to , and it's surrounding space


                          • -v FS=' *: *' set field separator to : and it's surrounding space


                          • '$1=="KEY2"print $2' print value if key found. change "KEY2" to your desired key value.





                          share|improve this answer




























                            0














                            0










                            0









                            Use awk, read key:value pair as record, read key, value as 1st and 2nd field.



                            awk -v RS=' *, *' -v FS=' *: *' '$1=="KEY2"print $2' <<<$str



                            • -v RS=' *, *' set record separator to , and it's surrounding space


                            • -v FS=' *: *' set field separator to : and it's surrounding space


                            • '$1=="KEY2"print $2' print value if key found. change "KEY2" to your desired key value.





                            share|improve this answer














                            Use awk, read key:value pair as record, read key, value as 1st and 2nd field.



                            awk -v RS=' *, *' -v FS=' *: *' '$1=="KEY2"print $2' <<<$str



                            • -v RS=' *, *' set record separator to , and it's surrounding space


                            • -v FS=' *: *' set field separator to : and it's surrounding space


                            • '$1=="KEY2"print $2' print value if key found. change "KEY2" to your desired key value.






                            share|improve this answer













                            share|improve this answer




                            share|improve this answer










                            answered May 19 at 11:21









                            dedowsdidedowsdi

                            8702 silver badges9 bronze badges




                            8702 silver badges9 bronze badges































                                draft saved

                                draft discarded















































                                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

                                Tamil (spriik) Luke uk diar | Nawigatjuun

                                Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

                                Where does the image of a data connector as a sharp metal spike originate from?Where does the concept of infected people turning into zombies only after death originate from?Where does the motif of a reanimated human head originate?Where did the notion that Dragons could speak originate?Where does the archetypal image of the 'Grey' alien come from?Where did the suffix '-Man' originate?Where does the notion of being injured or killed by an illusion originate?Where did the term “sophont” originate?Where does the trope of magic spells being driven by advanced technology originate from?Where did the term “the living impaired” originate?