Reading properties file in shell script containing “=” character












0














I have two files param.properties and readParam.sh which reads parameter values from param.properties file



param.properties:



val1=a
val2=b=c;c=d


readParam.sh:



PROPERTY_FILE=p.properties

function getProperty {
PROP_KEY=$1
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
echo $PROP_VALUE
}
val1=$(getProperty "val1")
echo val1 : $val1
val2=$(getProperty "val2")
echo val2 : $val2


When I run script readParam.sh I get output as:



val1 : a



val2 : b



Where, val2 gives only half value(it gives only "b" instead of "b=c;c=d")



Please help me with the same










share|improve this question



























    0














    I have two files param.properties and readParam.sh which reads parameter values from param.properties file



    param.properties:



    val1=a
    val2=b=c;c=d


    readParam.sh:



    PROPERTY_FILE=p.properties

    function getProperty {
    PROP_KEY=$1
    PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
    echo $PROP_VALUE
    }
    val1=$(getProperty "val1")
    echo val1 : $val1
    val2=$(getProperty "val2")
    echo val2 : $val2


    When I run script readParam.sh I get output as:



    val1 : a



    val2 : b



    Where, val2 gives only half value(it gives only "b" instead of "b=c;c=d")



    Please help me with the same










    share|improve this question

























      0












      0








      0







      I have two files param.properties and readParam.sh which reads parameter values from param.properties file



      param.properties:



      val1=a
      val2=b=c;c=d


      readParam.sh:



      PROPERTY_FILE=p.properties

      function getProperty {
      PROP_KEY=$1
      PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
      echo $PROP_VALUE
      }
      val1=$(getProperty "val1")
      echo val1 : $val1
      val2=$(getProperty "val2")
      echo val2 : $val2


      When I run script readParam.sh I get output as:



      val1 : a



      val2 : b



      Where, val2 gives only half value(it gives only "b" instead of "b=c;c=d")



      Please help me with the same










      share|improve this question













      I have two files param.properties and readParam.sh which reads parameter values from param.properties file



      param.properties:



      val1=a
      val2=b=c;c=d


      readParam.sh:



      PROPERTY_FILE=p.properties

      function getProperty {
      PROP_KEY=$1
      PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
      echo $PROP_VALUE
      }
      val1=$(getProperty "val1")
      echo val1 : $val1
      val2=$(getProperty "val2")
      echo val2 : $val2


      When I run script readParam.sh I get output as:



      val1 : a



      val2 : b



      Where, val2 gives only half value(it gives only "b" instead of "b=c;c=d")



      Please help me with the same







      shell properties-file






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 9:27









      JAVA_GEEK

      154




      154
























          2 Answers
          2






          active

          oldest

          votes


















          1














          just extend your existing codes:



          change



          cut -d'=' -f2


          into:



          cut -d'=' -f2-


          However I would change your cat|grep|cut chain into a single awk one-liner:



          function getProperty {
          awk -F'=' -v k="$1" '$1==k&&sub(/^[^=]*=/,"")' $PROPERTY_FILE
          }





          share|improve this answer































            1














            PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`


            -f2 will give you only the second parameter from the string you're cutting based on the delimiter.



            since "=" is present in multiple places in a same line it will split the string in to multiple part and when you try to retrive the output using f2 is will get you only the second part.



            so use to get the rest of the values is the string after first occurrence of the delimiter.



            PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2-`





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              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: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              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%2fstackoverflow.com%2fquestions%2f53408886%2freading-properties-file-in-shell-script-containing-character%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









              1














              just extend your existing codes:



              change



              cut -d'=' -f2


              into:



              cut -d'=' -f2-


              However I would change your cat|grep|cut chain into a single awk one-liner:



              function getProperty {
              awk -F'=' -v k="$1" '$1==k&&sub(/^[^=]*=/,"")' $PROPERTY_FILE
              }





              share|improve this answer




























                1














                just extend your existing codes:



                change



                cut -d'=' -f2


                into:



                cut -d'=' -f2-


                However I would change your cat|grep|cut chain into a single awk one-liner:



                function getProperty {
                awk -F'=' -v k="$1" '$1==k&&sub(/^[^=]*=/,"")' $PROPERTY_FILE
                }





                share|improve this answer


























                  1












                  1








                  1






                  just extend your existing codes:



                  change



                  cut -d'=' -f2


                  into:



                  cut -d'=' -f2-


                  However I would change your cat|grep|cut chain into a single awk one-liner:



                  function getProperty {
                  awk -F'=' -v k="$1" '$1==k&&sub(/^[^=]*=/,"")' $PROPERTY_FILE
                  }





                  share|improve this answer














                  just extend your existing codes:



                  change



                  cut -d'=' -f2


                  into:



                  cut -d'=' -f2-


                  However I would change your cat|grep|cut chain into a single awk one-liner:



                  function getProperty {
                  awk -F'=' -v k="$1" '$1==k&&sub(/^[^=]*=/,"")' $PROPERTY_FILE
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 21 at 9:45

























                  answered Nov 21 at 9:32









                  Kent

                  143k25153213




                  143k25153213

























                      1














                      PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`


                      -f2 will give you only the second parameter from the string you're cutting based on the delimiter.



                      since "=" is present in multiple places in a same line it will split the string in to multiple part and when you try to retrive the output using f2 is will get you only the second part.



                      so use to get the rest of the values is the string after first occurrence of the delimiter.



                      PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2-`





                      share|improve this answer


























                        1














                        PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`


                        -f2 will give you only the second parameter from the string you're cutting based on the delimiter.



                        since "=" is present in multiple places in a same line it will split the string in to multiple part and when you try to retrive the output using f2 is will get you only the second part.



                        so use to get the rest of the values is the string after first occurrence of the delimiter.



                        PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2-`





                        share|improve this answer
























                          1












                          1








                          1






                          PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`


                          -f2 will give you only the second parameter from the string you're cutting based on the delimiter.



                          since "=" is present in multiple places in a same line it will split the string in to multiple part and when you try to retrive the output using f2 is will get you only the second part.



                          so use to get the rest of the values is the string after first occurrence of the delimiter.



                          PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2-`





                          share|improve this answer












                          PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`


                          -f2 will give you only the second parameter from the string you're cutting based on the delimiter.



                          since "=" is present in multiple places in a same line it will split the string in to multiple part and when you try to retrive the output using f2 is will get you only the second part.



                          so use to get the rest of the values is the string after first occurrence of the delimiter.



                          PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2-`






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 21 at 9:59









                          Sekar Ramu

                          1649




                          1649






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • 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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2fstackoverflow.com%2fquestions%2f53408886%2freading-properties-file-in-shell-script-containing-character%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

                              404 Error Contact Form 7 ajax form submitting

                              How to know if a Active Directory user can login interactively

                              Refactoring coordinates for Minecraft Pi buildings written in Python