Bash - Extract key value by name from arbitrary text?












0















I'd like to have a simple script that lets me pass it any text, and it pulls out a value for a key from the string.



I'd like this to be flexible, and accept XML or JSON input, and even poorly formed input, like text from a log.



e.g. given any of the following inputs, it should be able to pull out the value of the test key.



e.g.



$ echo "test:5 hi there" | extract_key_value test



should result in



5


Note that I don't care what it's written in, so node, ruby, etc is fine by me, but portability (Linux/osx) is nice ;-)



input1



this is test:5 i saw a value


input2



this is test:'another value' i saw a value


input3



this is test=5 i saw a value


input4



test='a string value here'


input5



my data
on line 2 test='a string value here'
more data


My quick crack at this is the following, which I feel can be improved greatly and feels like it should be solved somewhere!



extract_key_value



#!/usr/bin/env bash

function show_help()
{
IT=$(cat <<EOF

Helps you extract a key value from a string, typically a log msg

usage: key {keyBeginDelim} {keyEndDelim}

e.g. given "asd f asdf asdf test=easy asdf me=you" as input

extract_key_value test
=> returns easy
EOF
)
echo "$IT"
exit
}

if [ "$1" == "help" ]
then
show_help
fi
if [ -z "$1" ]
then
show_help
fi

INPUT=$(cat -)
KEY="$1"

function getVal()
{
DELIM1="$1"
DELIM2="$2"
echo "$INPUT" | awk -F "$DELIM1" '{print $2}' | awk -F "$DELIM2" '{print $1}'
}

# Try whatever the user passed in or defaults for delims
if [ -n "$2" ]
then
IT=$(getVal "$2" "$3")
fi

# Try other use cases
if [ -z "$IT" ]
then
IT=$(getVal "$KEY:'" "'")
fi
if [ -z "$IT" ]
then
IT=$(getVal "$KEY='" "'")
fi
if [ -z "$IT" ]
then
IT=$(getVal "$KEY="" """)
fi
if [ -z "$IT" ]
then
IT=$(getVal "$KEY:"" """)
fi
if [ -z "$IT" ]
then
IT=$(getVal "$KEY:" " ")
fi
if [ -z "$IT" ]
then
IT=$(getVal "$KEY=" " ")
fi
if [ -z "$IT" ]
then
IT=$(getVal "$KEY=" ";")
fi
if [ -z "$IT" ]
then
IT=$(getVal "$KEY:" ";")
fi

echo "$IT"









share|improve this question





























    0















    I'd like to have a simple script that lets me pass it any text, and it pulls out a value for a key from the string.



    I'd like this to be flexible, and accept XML or JSON input, and even poorly formed input, like text from a log.



    e.g. given any of the following inputs, it should be able to pull out the value of the test key.



    e.g.



    $ echo "test:5 hi there" | extract_key_value test



    should result in



    5


    Note that I don't care what it's written in, so node, ruby, etc is fine by me, but portability (Linux/osx) is nice ;-)



    input1



    this is test:5 i saw a value


    input2



    this is test:'another value' i saw a value


    input3



    this is test=5 i saw a value


    input4



    test='a string value here'


    input5



    my data
    on line 2 test='a string value here'
    more data


    My quick crack at this is the following, which I feel can be improved greatly and feels like it should be solved somewhere!



    extract_key_value



    #!/usr/bin/env bash

    function show_help()
    {
    IT=$(cat <<EOF

    Helps you extract a key value from a string, typically a log msg

    usage: key {keyBeginDelim} {keyEndDelim}

    e.g. given "asd f asdf asdf test=easy asdf me=you" as input

    extract_key_value test
    => returns easy
    EOF
    )
    echo "$IT"
    exit
    }

    if [ "$1" == "help" ]
    then
    show_help
    fi
    if [ -z "$1" ]
    then
    show_help
    fi

    INPUT=$(cat -)
    KEY="$1"

    function getVal()
    {
    DELIM1="$1"
    DELIM2="$2"
    echo "$INPUT" | awk -F "$DELIM1" '{print $2}' | awk -F "$DELIM2" '{print $1}'
    }

    # Try whatever the user passed in or defaults for delims
    if [ -n "$2" ]
    then
    IT=$(getVal "$2" "$3")
    fi

    # Try other use cases
    if [ -z "$IT" ]
    then
    IT=$(getVal "$KEY:'" "'")
    fi
    if [ -z "$IT" ]
    then
    IT=$(getVal "$KEY='" "'")
    fi
    if [ -z "$IT" ]
    then
    IT=$(getVal "$KEY="" """)
    fi
    if [ -z "$IT" ]
    then
    IT=$(getVal "$KEY:"" """)
    fi
    if [ -z "$IT" ]
    then
    IT=$(getVal "$KEY:" " ")
    fi
    if [ -z "$IT" ]
    then
    IT=$(getVal "$KEY=" " ")
    fi
    if [ -z "$IT" ]
    then
    IT=$(getVal "$KEY=" ";")
    fi
    if [ -z "$IT" ]
    then
    IT=$(getVal "$KEY:" ";")
    fi

    echo "$IT"









    share|improve this question



























      0












      0








      0








      I'd like to have a simple script that lets me pass it any text, and it pulls out a value for a key from the string.



      I'd like this to be flexible, and accept XML or JSON input, and even poorly formed input, like text from a log.



      e.g. given any of the following inputs, it should be able to pull out the value of the test key.



      e.g.



      $ echo "test:5 hi there" | extract_key_value test



      should result in



      5


      Note that I don't care what it's written in, so node, ruby, etc is fine by me, but portability (Linux/osx) is nice ;-)



      input1



      this is test:5 i saw a value


      input2



      this is test:'another value' i saw a value


      input3



      this is test=5 i saw a value


      input4



      test='a string value here'


      input5



      my data
      on line 2 test='a string value here'
      more data


      My quick crack at this is the following, which I feel can be improved greatly and feels like it should be solved somewhere!



      extract_key_value



      #!/usr/bin/env bash

      function show_help()
      {
      IT=$(cat <<EOF

      Helps you extract a key value from a string, typically a log msg

      usage: key {keyBeginDelim} {keyEndDelim}

      e.g. given "asd f asdf asdf test=easy asdf me=you" as input

      extract_key_value test
      => returns easy
      EOF
      )
      echo "$IT"
      exit
      }

      if [ "$1" == "help" ]
      then
      show_help
      fi
      if [ -z "$1" ]
      then
      show_help
      fi

      INPUT=$(cat -)
      KEY="$1"

      function getVal()
      {
      DELIM1="$1"
      DELIM2="$2"
      echo "$INPUT" | awk -F "$DELIM1" '{print $2}' | awk -F "$DELIM2" '{print $1}'
      }

      # Try whatever the user passed in or defaults for delims
      if [ -n "$2" ]
      then
      IT=$(getVal "$2" "$3")
      fi

      # Try other use cases
      if [ -z "$IT" ]
      then
      IT=$(getVal "$KEY:'" "'")
      fi
      if [ -z "$IT" ]
      then
      IT=$(getVal "$KEY='" "'")
      fi
      if [ -z "$IT" ]
      then
      IT=$(getVal "$KEY="" """)
      fi
      if [ -z "$IT" ]
      then
      IT=$(getVal "$KEY:"" """)
      fi
      if [ -z "$IT" ]
      then
      IT=$(getVal "$KEY:" " ")
      fi
      if [ -z "$IT" ]
      then
      IT=$(getVal "$KEY=" " ")
      fi
      if [ -z "$IT" ]
      then
      IT=$(getVal "$KEY=" ";")
      fi
      if [ -z "$IT" ]
      then
      IT=$(getVal "$KEY:" ";")
      fi

      echo "$IT"









      share|improve this question
















      I'd like to have a simple script that lets me pass it any text, and it pulls out a value for a key from the string.



      I'd like this to be flexible, and accept XML or JSON input, and even poorly formed input, like text from a log.



      e.g. given any of the following inputs, it should be able to pull out the value of the test key.



      e.g.



      $ echo "test:5 hi there" | extract_key_value test



      should result in



      5


      Note that I don't care what it's written in, so node, ruby, etc is fine by me, but portability (Linux/osx) is nice ;-)



      input1



      this is test:5 i saw a value


      input2



      this is test:'another value' i saw a value


      input3



      this is test=5 i saw a value


      input4



      test='a string value here'


      input5



      my data
      on line 2 test='a string value here'
      more data


      My quick crack at this is the following, which I feel can be improved greatly and feels like it should be solved somewhere!



      extract_key_value



      #!/usr/bin/env bash

      function show_help()
      {
      IT=$(cat <<EOF

      Helps you extract a key value from a string, typically a log msg

      usage: key {keyBeginDelim} {keyEndDelim}

      e.g. given "asd f asdf asdf test=easy asdf me=you" as input

      extract_key_value test
      => returns easy
      EOF
      )
      echo "$IT"
      exit
      }

      if [ "$1" == "help" ]
      then
      show_help
      fi
      if [ -z "$1" ]
      then
      show_help
      fi

      INPUT=$(cat -)
      KEY="$1"

      function getVal()
      {
      DELIM1="$1"
      DELIM2="$2"
      echo "$INPUT" | awk -F "$DELIM1" '{print $2}' | awk -F "$DELIM2" '{print $1}'
      }

      # Try whatever the user passed in or defaults for delims
      if [ -n "$2" ]
      then
      IT=$(getVal "$2" "$3")
      fi

      # Try other use cases
      if [ -z "$IT" ]
      then
      IT=$(getVal "$KEY:'" "'")
      fi
      if [ -z "$IT" ]
      then
      IT=$(getVal "$KEY='" "'")
      fi
      if [ -z "$IT" ]
      then
      IT=$(getVal "$KEY="" """)
      fi
      if [ -z "$IT" ]
      then
      IT=$(getVal "$KEY:"" """)
      fi
      if [ -z "$IT" ]
      then
      IT=$(getVal "$KEY:" " ")
      fi
      if [ -z "$IT" ]
      then
      IT=$(getVal "$KEY=" " ")
      fi
      if [ -z "$IT" ]
      then
      IT=$(getVal "$KEY=" ";")
      fi
      if [ -z "$IT" ]
      then
      IT=$(getVal "$KEY:" ";")
      fi

      echo "$IT"






      bash






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 4 at 15:01









      msp9011

      4,31844065




      4,31844065










      asked Feb 1 at 13:39









      Brad ParksBrad Parks

      4371623




      4371623






















          2 Answers
          2






          active

          oldest

          votes


















          4














          With pcregrep:



          extract_key_value() {
          pcregrep -Mo1 "(?sx)
          (?:
          Q$1E # key literally
          | "Q$1E" # same in double quotes
          | 'Q$1E' # same in single quotes
          )
          [=:]
          (?| # branch reset
          '(.*?)'
          | "(.*?)"
          | ([^"'s]+)
          )"
          }




          • -M: multiline match (to allow test:'foonbar'...)


          • -o1: output the text matched by the first capture group (see below about branch reset).


          • (?sx): enable the s flag (make . match newline characters as well) and x flag (allow that multiline with comment format)


          • Q$1E the content of $1 (the first argument to the function) is to be taken literally. That assumes it doesn't contain E itself. In ksh93-like shells like bash, you can replace $1 with ${1//\E/\E\\E\Q} to work around that.


          • (?|.(.).|.(.).) branch reset. The numbering of the capture group starts from 1 after each |, so -o1 will return the first capture group that matches in any of the alternation.


          • '.*?'. .*? is the non-greedy variant of .*, so '.*' will match from ' to the first ' after that.


          • s: any whitespace character.


          That doesn't try to address corner cases like the x encodings in json, the embedding of quotes within quotes (which is done differently depending on the language). It doesn't allow whitespace on either side of the : or =. All those can be addressed if need be. That will depend on the type of exact input you're trying to process.






          share|improve this answer


























          • sweet! this seems great... one other use case occurred to me - that being that the KEY itself is quoted, matching whatever the value has... e.g. "test":"5" or 'test':'5' ... thoughts? thanks regardless!

            – Brad Parks
            Feb 1 at 14:51








          • 1





            @Brad, see edit.

            – Stéphane Chazelas
            Feb 1 at 14:57











          • That totally works - there was one minor fix required, that being that for the single quote case, the $q should be $1... thanks a bunch Stéphane Chazelas !

            – Brad Parks
            Feb 1 at 15:14






          • 1





            @Brad, thanks. I've now fixed the $q typo.

            – Stéphane Chazelas
            Feb 1 at 15:19



















          3














          A example with grep:



          function extract_key_value() {
          egrep -o "$1[:=]['"[:alnum:]]+" | egrep -o "['"[:alnum:]]+$" | egrep -o "[[:alnum:]]+"
          }


          echo -e "on line 1ntest:123 asasasnon line 3ntest='abc'non line 5" | extract_key_value test





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


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f498133%2fbash-extract-key-value-by-name-from-arbitrary-text%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









            4














            With pcregrep:



            extract_key_value() {
            pcregrep -Mo1 "(?sx)
            (?:
            Q$1E # key literally
            | "Q$1E" # same in double quotes
            | 'Q$1E' # same in single quotes
            )
            [=:]
            (?| # branch reset
            '(.*?)'
            | "(.*?)"
            | ([^"'s]+)
            )"
            }




            • -M: multiline match (to allow test:'foonbar'...)


            • -o1: output the text matched by the first capture group (see below about branch reset).


            • (?sx): enable the s flag (make . match newline characters as well) and x flag (allow that multiline with comment format)


            • Q$1E the content of $1 (the first argument to the function) is to be taken literally. That assumes it doesn't contain E itself. In ksh93-like shells like bash, you can replace $1 with ${1//\E/\E\\E\Q} to work around that.


            • (?|.(.).|.(.).) branch reset. The numbering of the capture group starts from 1 after each |, so -o1 will return the first capture group that matches in any of the alternation.


            • '.*?'. .*? is the non-greedy variant of .*, so '.*' will match from ' to the first ' after that.


            • s: any whitespace character.


            That doesn't try to address corner cases like the x encodings in json, the embedding of quotes within quotes (which is done differently depending on the language). It doesn't allow whitespace on either side of the : or =. All those can be addressed if need be. That will depend on the type of exact input you're trying to process.






            share|improve this answer


























            • sweet! this seems great... one other use case occurred to me - that being that the KEY itself is quoted, matching whatever the value has... e.g. "test":"5" or 'test':'5' ... thoughts? thanks regardless!

              – Brad Parks
              Feb 1 at 14:51








            • 1





              @Brad, see edit.

              – Stéphane Chazelas
              Feb 1 at 14:57











            • That totally works - there was one minor fix required, that being that for the single quote case, the $q should be $1... thanks a bunch Stéphane Chazelas !

              – Brad Parks
              Feb 1 at 15:14






            • 1





              @Brad, thanks. I've now fixed the $q typo.

              – Stéphane Chazelas
              Feb 1 at 15:19
















            4














            With pcregrep:



            extract_key_value() {
            pcregrep -Mo1 "(?sx)
            (?:
            Q$1E # key literally
            | "Q$1E" # same in double quotes
            | 'Q$1E' # same in single quotes
            )
            [=:]
            (?| # branch reset
            '(.*?)'
            | "(.*?)"
            | ([^"'s]+)
            )"
            }




            • -M: multiline match (to allow test:'foonbar'...)


            • -o1: output the text matched by the first capture group (see below about branch reset).


            • (?sx): enable the s flag (make . match newline characters as well) and x flag (allow that multiline with comment format)


            • Q$1E the content of $1 (the first argument to the function) is to be taken literally. That assumes it doesn't contain E itself. In ksh93-like shells like bash, you can replace $1 with ${1//\E/\E\\E\Q} to work around that.


            • (?|.(.).|.(.).) branch reset. The numbering of the capture group starts from 1 after each |, so -o1 will return the first capture group that matches in any of the alternation.


            • '.*?'. .*? is the non-greedy variant of .*, so '.*' will match from ' to the first ' after that.


            • s: any whitespace character.


            That doesn't try to address corner cases like the x encodings in json, the embedding of quotes within quotes (which is done differently depending on the language). It doesn't allow whitespace on either side of the : or =. All those can be addressed if need be. That will depend on the type of exact input you're trying to process.






            share|improve this answer


























            • sweet! this seems great... one other use case occurred to me - that being that the KEY itself is quoted, matching whatever the value has... e.g. "test":"5" or 'test':'5' ... thoughts? thanks regardless!

              – Brad Parks
              Feb 1 at 14:51








            • 1





              @Brad, see edit.

              – Stéphane Chazelas
              Feb 1 at 14:57











            • That totally works - there was one minor fix required, that being that for the single quote case, the $q should be $1... thanks a bunch Stéphane Chazelas !

              – Brad Parks
              Feb 1 at 15:14






            • 1





              @Brad, thanks. I've now fixed the $q typo.

              – Stéphane Chazelas
              Feb 1 at 15:19














            4












            4








            4







            With pcregrep:



            extract_key_value() {
            pcregrep -Mo1 "(?sx)
            (?:
            Q$1E # key literally
            | "Q$1E" # same in double quotes
            | 'Q$1E' # same in single quotes
            )
            [=:]
            (?| # branch reset
            '(.*?)'
            | "(.*?)"
            | ([^"'s]+)
            )"
            }




            • -M: multiline match (to allow test:'foonbar'...)


            • -o1: output the text matched by the first capture group (see below about branch reset).


            • (?sx): enable the s flag (make . match newline characters as well) and x flag (allow that multiline with comment format)


            • Q$1E the content of $1 (the first argument to the function) is to be taken literally. That assumes it doesn't contain E itself. In ksh93-like shells like bash, you can replace $1 with ${1//\E/\E\\E\Q} to work around that.


            • (?|.(.).|.(.).) branch reset. The numbering of the capture group starts from 1 after each |, so -o1 will return the first capture group that matches in any of the alternation.


            • '.*?'. .*? is the non-greedy variant of .*, so '.*' will match from ' to the first ' after that.


            • s: any whitespace character.


            That doesn't try to address corner cases like the x encodings in json, the embedding of quotes within quotes (which is done differently depending on the language). It doesn't allow whitespace on either side of the : or =. All those can be addressed if need be. That will depend on the type of exact input you're trying to process.






            share|improve this answer















            With pcregrep:



            extract_key_value() {
            pcregrep -Mo1 "(?sx)
            (?:
            Q$1E # key literally
            | "Q$1E" # same in double quotes
            | 'Q$1E' # same in single quotes
            )
            [=:]
            (?| # branch reset
            '(.*?)'
            | "(.*?)"
            | ([^"'s]+)
            )"
            }




            • -M: multiline match (to allow test:'foonbar'...)


            • -o1: output the text matched by the first capture group (see below about branch reset).


            • (?sx): enable the s flag (make . match newline characters as well) and x flag (allow that multiline with comment format)


            • Q$1E the content of $1 (the first argument to the function) is to be taken literally. That assumes it doesn't contain E itself. In ksh93-like shells like bash, you can replace $1 with ${1//\E/\E\\E\Q} to work around that.


            • (?|.(.).|.(.).) branch reset. The numbering of the capture group starts from 1 after each |, so -o1 will return the first capture group that matches in any of the alternation.


            • '.*?'. .*? is the non-greedy variant of .*, so '.*' will match from ' to the first ' after that.


            • s: any whitespace character.


            That doesn't try to address corner cases like the x encodings in json, the embedding of quotes within quotes (which is done differently depending on the language). It doesn't allow whitespace on either side of the : or =. All those can be addressed if need be. That will depend on the type of exact input you're trying to process.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 1 at 15:24

























            answered Feb 1 at 14:23









            Stéphane ChazelasStéphane Chazelas

            305k57575929




            305k57575929













            • sweet! this seems great... one other use case occurred to me - that being that the KEY itself is quoted, matching whatever the value has... e.g. "test":"5" or 'test':'5' ... thoughts? thanks regardless!

              – Brad Parks
              Feb 1 at 14:51








            • 1





              @Brad, see edit.

              – Stéphane Chazelas
              Feb 1 at 14:57











            • That totally works - there was one minor fix required, that being that for the single quote case, the $q should be $1... thanks a bunch Stéphane Chazelas !

              – Brad Parks
              Feb 1 at 15:14






            • 1





              @Brad, thanks. I've now fixed the $q typo.

              – Stéphane Chazelas
              Feb 1 at 15:19



















            • sweet! this seems great... one other use case occurred to me - that being that the KEY itself is quoted, matching whatever the value has... e.g. "test":"5" or 'test':'5' ... thoughts? thanks regardless!

              – Brad Parks
              Feb 1 at 14:51








            • 1





              @Brad, see edit.

              – Stéphane Chazelas
              Feb 1 at 14:57











            • That totally works - there was one minor fix required, that being that for the single quote case, the $q should be $1... thanks a bunch Stéphane Chazelas !

              – Brad Parks
              Feb 1 at 15:14






            • 1





              @Brad, thanks. I've now fixed the $q typo.

              – Stéphane Chazelas
              Feb 1 at 15:19

















            sweet! this seems great... one other use case occurred to me - that being that the KEY itself is quoted, matching whatever the value has... e.g. "test":"5" or 'test':'5' ... thoughts? thanks regardless!

            – Brad Parks
            Feb 1 at 14:51







            sweet! this seems great... one other use case occurred to me - that being that the KEY itself is quoted, matching whatever the value has... e.g. "test":"5" or 'test':'5' ... thoughts? thanks regardless!

            – Brad Parks
            Feb 1 at 14:51






            1




            1





            @Brad, see edit.

            – Stéphane Chazelas
            Feb 1 at 14:57





            @Brad, see edit.

            – Stéphane Chazelas
            Feb 1 at 14:57













            That totally works - there was one minor fix required, that being that for the single quote case, the $q should be $1... thanks a bunch Stéphane Chazelas !

            – Brad Parks
            Feb 1 at 15:14





            That totally works - there was one minor fix required, that being that for the single quote case, the $q should be $1... thanks a bunch Stéphane Chazelas !

            – Brad Parks
            Feb 1 at 15:14




            1




            1





            @Brad, thanks. I've now fixed the $q typo.

            – Stéphane Chazelas
            Feb 1 at 15:19





            @Brad, thanks. I've now fixed the $q typo.

            – Stéphane Chazelas
            Feb 1 at 15:19













            3














            A example with grep:



            function extract_key_value() {
            egrep -o "$1[:=]['"[:alnum:]]+" | egrep -o "['"[:alnum:]]+$" | egrep -o "[[:alnum:]]+"
            }


            echo -e "on line 1ntest:123 asasasnon line 3ntest='abc'non line 5" | extract_key_value test





            share|improve this answer






























              3














              A example with grep:



              function extract_key_value() {
              egrep -o "$1[:=]['"[:alnum:]]+" | egrep -o "['"[:alnum:]]+$" | egrep -o "[[:alnum:]]+"
              }


              echo -e "on line 1ntest:123 asasasnon line 3ntest='abc'non line 5" | extract_key_value test





              share|improve this answer




























                3












                3








                3







                A example with grep:



                function extract_key_value() {
                egrep -o "$1[:=]['"[:alnum:]]+" | egrep -o "['"[:alnum:]]+$" | egrep -o "[[:alnum:]]+"
                }


                echo -e "on line 1ntest:123 asasasnon line 3ntest='abc'non line 5" | extract_key_value test





                share|improve this answer















                A example with grep:



                function extract_key_value() {
                egrep -o "$1[:=]['"[:alnum:]]+" | egrep -o "['"[:alnum:]]+$" | egrep -o "[[:alnum:]]+"
                }


                echo -e "on line 1ntest:123 asasasnon line 3ntest='abc'non line 5" | extract_key_value test






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 1 at 15:22

























                answered Feb 1 at 14:55









                Thiago LuísThiago Luís

                312




                312






























                    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%2f498133%2fbash-extract-key-value-by-name-from-arbitrary-text%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

                    How to make a Squid Proxy server?

                    Is this a new Fibonacci Identity?

                    19世紀