Bash one liner to change configuration parameters












4














I have a config file with following structure.



ValueOne = 1
ValueTwo = 2
ValueThree = 3


I want a one liner bash script to find ValueTwo and change the value to 22222.



Any idea?
Not School Thing










share|improve this question





























    4














    I have a config file with following structure.



    ValueOne = 1
    ValueTwo = 2
    ValueThree = 3


    I want a one liner bash script to find ValueTwo and change the value to 22222.



    Any idea?
    Not School Thing










    share|improve this question



























      4












      4








      4


      3





      I have a config file with following structure.



      ValueOne = 1
      ValueTwo = 2
      ValueThree = 3


      I want a one liner bash script to find ValueTwo and change the value to 22222.



      Any idea?
      Not School Thing










      share|improve this question















      I have a config file with following structure.



      ValueOne = 1
      ValueTwo = 2
      ValueThree = 3


      I want a one liner bash script to find ValueTwo and change the value to 22222.



      Any idea?
      Not School Thing







      shell-script text-processing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 16 '14 at 14:08









      terdon

      128k31249423




      128k31249423










      asked Jul 16 '14 at 13:36









      TRA

      2314




      2314






















          4 Answers
          4






          active

          oldest

          votes


















          5














          I bet there'll be better ones but here's my go:



          If the config file has parameters on their own line



          sed -i '/ValueTwo/s/= .*/= 22222/' config_file




          • /ValueTwo/ : Search for the string ValueTwo to find which line to operate on (Addresses)


          • s/= .*/= 22222/ : On the lines that match the search above, substitute = .* for = 22222 (Substitute)


          • = .* : Search for the = character followed by a space () character followed by 0 or more of any character (.*) (Regex example)


          • = 22222 : Replace what's found with the literal string = 22222


          This will replace the contents of config_file in-place. To create a new file with the parameter changed, remove -i and place > new_file at the end of the line.





          If your config file has parameters on the same line (like the unedited question):



          sed -i 's/ValueTwo = [^ ]*/ValueTwo = 22222/' config_file


          This will replace the contents of config_file in-place as well. It will work as long as there are no spaces in the parameter for ValueTwo. This will also work in the case where parameters are on their own line, but the former method is perhaps more robust in that case.






          share|improve this answer



















          • 4




            I think you lost your bet.
            – mikeserv
            Sep 9 '14 at 16:05






          • 1




            That is some RegEx'ish mumbo jumbo, would love an explanation /s/=?
            – Ray Foss
            May 17 '16 at 14:16



















          2














          perl -p -i.bak -e 's/ValueTwo = 2/ValueTwo = 22222/' path/to/configfile


          will edit the file in-place and save a copy of the original in case of finger trouble. You can do the same with awk.






          share|improve this answer





























            1














            Assuming ValueTwo is a number, sed will do just fine:



            sed -e 's/ValueTwo = [0-9]*/ValueTwo = 2222/g' your_config_file > output_file





            share|improve this answer





















            • +1 but why would you limit it to numbers? What's wrong with .*?
              – terdon
              Jul 16 '14 at 14:12










            • @terdon, in the first version of the question all variables were in one line, separated by whitespaces. Also it is a good practice to limit the scope of regular expression to values that you expect to find - this way you can filter out some unexpected errors - e.g. when a text is logged instead of a number.
              – Paweł Rumian
              Jul 16 '14 at 14:21



















            1














            I'd go for awk:



            awk '/ValueTwo/{$3=22222}1;' file > newfile


            The above checks whether a given line matches ValueTwo and sets the 3d field to 222 on matching lines. The 1; is just an awk shgorthand way of writing print $0, it will print each line. Since it is outside the match block (/ValueTwo/{}), it will cause all lines to be printed.



            Since you asked for a bash solution though (don't know why you would prefer one but still), you could try this:



            while read key eq val; do
            [ $key = "ValueTwo" ] && val=22222
            printf "%s %s %sn" $key $eq $val
            done < file





            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%2f144846%2fbash-one-liner-to-change-configuration-parameters%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









              5














              I bet there'll be better ones but here's my go:



              If the config file has parameters on their own line



              sed -i '/ValueTwo/s/= .*/= 22222/' config_file




              • /ValueTwo/ : Search for the string ValueTwo to find which line to operate on (Addresses)


              • s/= .*/= 22222/ : On the lines that match the search above, substitute = .* for = 22222 (Substitute)


              • = .* : Search for the = character followed by a space () character followed by 0 or more of any character (.*) (Regex example)


              • = 22222 : Replace what's found with the literal string = 22222


              This will replace the contents of config_file in-place. To create a new file with the parameter changed, remove -i and place > new_file at the end of the line.





              If your config file has parameters on the same line (like the unedited question):



              sed -i 's/ValueTwo = [^ ]*/ValueTwo = 22222/' config_file


              This will replace the contents of config_file in-place as well. It will work as long as there are no spaces in the parameter for ValueTwo. This will also work in the case where parameters are on their own line, but the former method is perhaps more robust in that case.






              share|improve this answer



















              • 4




                I think you lost your bet.
                – mikeserv
                Sep 9 '14 at 16:05






              • 1




                That is some RegEx'ish mumbo jumbo, would love an explanation /s/=?
                – Ray Foss
                May 17 '16 at 14:16
















              5














              I bet there'll be better ones but here's my go:



              If the config file has parameters on their own line



              sed -i '/ValueTwo/s/= .*/= 22222/' config_file




              • /ValueTwo/ : Search for the string ValueTwo to find which line to operate on (Addresses)


              • s/= .*/= 22222/ : On the lines that match the search above, substitute = .* for = 22222 (Substitute)


              • = .* : Search for the = character followed by a space () character followed by 0 or more of any character (.*) (Regex example)


              • = 22222 : Replace what's found with the literal string = 22222


              This will replace the contents of config_file in-place. To create a new file with the parameter changed, remove -i and place > new_file at the end of the line.





              If your config file has parameters on the same line (like the unedited question):



              sed -i 's/ValueTwo = [^ ]*/ValueTwo = 22222/' config_file


              This will replace the contents of config_file in-place as well. It will work as long as there are no spaces in the parameter for ValueTwo. This will also work in the case where parameters are on their own line, but the former method is perhaps more robust in that case.






              share|improve this answer



















              • 4




                I think you lost your bet.
                – mikeserv
                Sep 9 '14 at 16:05






              • 1




                That is some RegEx'ish mumbo jumbo, would love an explanation /s/=?
                – Ray Foss
                May 17 '16 at 14:16














              5












              5








              5






              I bet there'll be better ones but here's my go:



              If the config file has parameters on their own line



              sed -i '/ValueTwo/s/= .*/= 22222/' config_file




              • /ValueTwo/ : Search for the string ValueTwo to find which line to operate on (Addresses)


              • s/= .*/= 22222/ : On the lines that match the search above, substitute = .* for = 22222 (Substitute)


              • = .* : Search for the = character followed by a space () character followed by 0 or more of any character (.*) (Regex example)


              • = 22222 : Replace what's found with the literal string = 22222


              This will replace the contents of config_file in-place. To create a new file with the parameter changed, remove -i and place > new_file at the end of the line.





              If your config file has parameters on the same line (like the unedited question):



              sed -i 's/ValueTwo = [^ ]*/ValueTwo = 22222/' config_file


              This will replace the contents of config_file in-place as well. It will work as long as there are no spaces in the parameter for ValueTwo. This will also work in the case where parameters are on their own line, but the former method is perhaps more robust in that case.






              share|improve this answer














              I bet there'll be better ones but here's my go:



              If the config file has parameters on their own line



              sed -i '/ValueTwo/s/= .*/= 22222/' config_file




              • /ValueTwo/ : Search for the string ValueTwo to find which line to operate on (Addresses)


              • s/= .*/= 22222/ : On the lines that match the search above, substitute = .* for = 22222 (Substitute)


              • = .* : Search for the = character followed by a space () character followed by 0 or more of any character (.*) (Regex example)


              • = 22222 : Replace what's found with the literal string = 22222


              This will replace the contents of config_file in-place. To create a new file with the parameter changed, remove -i and place > new_file at the end of the line.





              If your config file has parameters on the same line (like the unedited question):



              sed -i 's/ValueTwo = [^ ]*/ValueTwo = 22222/' config_file


              This will replace the contents of config_file in-place as well. It will work as long as there are no spaces in the parameter for ValueTwo. This will also work in the case where parameters are on their own line, but the former method is perhaps more robust in that case.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 5 mins ago









              gene_wood

              1054




              1054










              answered Jul 16 '14 at 13:47









              drs

              3,30352859




              3,30352859








              • 4




                I think you lost your bet.
                – mikeserv
                Sep 9 '14 at 16:05






              • 1




                That is some RegEx'ish mumbo jumbo, would love an explanation /s/=?
                – Ray Foss
                May 17 '16 at 14:16














              • 4




                I think you lost your bet.
                – mikeserv
                Sep 9 '14 at 16:05






              • 1




                That is some RegEx'ish mumbo jumbo, would love an explanation /s/=?
                – Ray Foss
                May 17 '16 at 14:16








              4




              4




              I think you lost your bet.
              – mikeserv
              Sep 9 '14 at 16:05




              I think you lost your bet.
              – mikeserv
              Sep 9 '14 at 16:05




              1




              1




              That is some RegEx'ish mumbo jumbo, would love an explanation /s/=?
              – Ray Foss
              May 17 '16 at 14:16




              That is some RegEx'ish mumbo jumbo, would love an explanation /s/=?
              – Ray Foss
              May 17 '16 at 14:16













              2














              perl -p -i.bak -e 's/ValueTwo = 2/ValueTwo = 22222/' path/to/configfile


              will edit the file in-place and save a copy of the original in case of finger trouble. You can do the same with awk.






              share|improve this answer


























                2














                perl -p -i.bak -e 's/ValueTwo = 2/ValueTwo = 22222/' path/to/configfile


                will edit the file in-place and save a copy of the original in case of finger trouble. You can do the same with awk.






                share|improve this answer
























                  2












                  2








                  2






                  perl -p -i.bak -e 's/ValueTwo = 2/ValueTwo = 22222/' path/to/configfile


                  will edit the file in-place and save a copy of the original in case of finger trouble. You can do the same with awk.






                  share|improve this answer












                  perl -p -i.bak -e 's/ValueTwo = 2/ValueTwo = 22222/' path/to/configfile


                  will edit the file in-place and save a copy of the original in case of finger trouble. You can do the same with awk.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 16 '14 at 13:45









                  RedGrittyBrick

                  1,6601519




                  1,6601519























                      1














                      Assuming ValueTwo is a number, sed will do just fine:



                      sed -e 's/ValueTwo = [0-9]*/ValueTwo = 2222/g' your_config_file > output_file





                      share|improve this answer





















                      • +1 but why would you limit it to numbers? What's wrong with .*?
                        – terdon
                        Jul 16 '14 at 14:12










                      • @terdon, in the first version of the question all variables were in one line, separated by whitespaces. Also it is a good practice to limit the scope of regular expression to values that you expect to find - this way you can filter out some unexpected errors - e.g. when a text is logged instead of a number.
                        – Paweł Rumian
                        Jul 16 '14 at 14:21
















                      1














                      Assuming ValueTwo is a number, sed will do just fine:



                      sed -e 's/ValueTwo = [0-9]*/ValueTwo = 2222/g' your_config_file > output_file





                      share|improve this answer





















                      • +1 but why would you limit it to numbers? What's wrong with .*?
                        – terdon
                        Jul 16 '14 at 14:12










                      • @terdon, in the first version of the question all variables were in one line, separated by whitespaces. Also it is a good practice to limit the scope of regular expression to values that you expect to find - this way you can filter out some unexpected errors - e.g. when a text is logged instead of a number.
                        – Paweł Rumian
                        Jul 16 '14 at 14:21














                      1












                      1








                      1






                      Assuming ValueTwo is a number, sed will do just fine:



                      sed -e 's/ValueTwo = [0-9]*/ValueTwo = 2222/g' your_config_file > output_file





                      share|improve this answer












                      Assuming ValueTwo is a number, sed will do just fine:



                      sed -e 's/ValueTwo = [0-9]*/ValueTwo = 2222/g' your_config_file > output_file






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jul 16 '14 at 13:51









                      Paweł Rumian

                      1,271720




                      1,271720












                      • +1 but why would you limit it to numbers? What's wrong with .*?
                        – terdon
                        Jul 16 '14 at 14:12










                      • @terdon, in the first version of the question all variables were in one line, separated by whitespaces. Also it is a good practice to limit the scope of regular expression to values that you expect to find - this way you can filter out some unexpected errors - e.g. when a text is logged instead of a number.
                        – Paweł Rumian
                        Jul 16 '14 at 14:21


















                      • +1 but why would you limit it to numbers? What's wrong with .*?
                        – terdon
                        Jul 16 '14 at 14:12










                      • @terdon, in the first version of the question all variables were in one line, separated by whitespaces. Also it is a good practice to limit the scope of regular expression to values that you expect to find - this way you can filter out some unexpected errors - e.g. when a text is logged instead of a number.
                        – Paweł Rumian
                        Jul 16 '14 at 14:21
















                      +1 but why would you limit it to numbers? What's wrong with .*?
                      – terdon
                      Jul 16 '14 at 14:12




                      +1 but why would you limit it to numbers? What's wrong with .*?
                      – terdon
                      Jul 16 '14 at 14:12












                      @terdon, in the first version of the question all variables were in one line, separated by whitespaces. Also it is a good practice to limit the scope of regular expression to values that you expect to find - this way you can filter out some unexpected errors - e.g. when a text is logged instead of a number.
                      – Paweł Rumian
                      Jul 16 '14 at 14:21




                      @terdon, in the first version of the question all variables were in one line, separated by whitespaces. Also it is a good practice to limit the scope of regular expression to values that you expect to find - this way you can filter out some unexpected errors - e.g. when a text is logged instead of a number.
                      – Paweł Rumian
                      Jul 16 '14 at 14:21











                      1














                      I'd go for awk:



                      awk '/ValueTwo/{$3=22222}1;' file > newfile


                      The above checks whether a given line matches ValueTwo and sets the 3d field to 222 on matching lines. The 1; is just an awk shgorthand way of writing print $0, it will print each line. Since it is outside the match block (/ValueTwo/{}), it will cause all lines to be printed.



                      Since you asked for a bash solution though (don't know why you would prefer one but still), you could try this:



                      while read key eq val; do
                      [ $key = "ValueTwo" ] && val=22222
                      printf "%s %s %sn" $key $eq $val
                      done < file





                      share|improve this answer


























                        1














                        I'd go for awk:



                        awk '/ValueTwo/{$3=22222}1;' file > newfile


                        The above checks whether a given line matches ValueTwo and sets the 3d field to 222 on matching lines. The 1; is just an awk shgorthand way of writing print $0, it will print each line. Since it is outside the match block (/ValueTwo/{}), it will cause all lines to be printed.



                        Since you asked for a bash solution though (don't know why you would prefer one but still), you could try this:



                        while read key eq val; do
                        [ $key = "ValueTwo" ] && val=22222
                        printf "%s %s %sn" $key $eq $val
                        done < file





                        share|improve this answer
























                          1












                          1








                          1






                          I'd go for awk:



                          awk '/ValueTwo/{$3=22222}1;' file > newfile


                          The above checks whether a given line matches ValueTwo and sets the 3d field to 222 on matching lines. The 1; is just an awk shgorthand way of writing print $0, it will print each line. Since it is outside the match block (/ValueTwo/{}), it will cause all lines to be printed.



                          Since you asked for a bash solution though (don't know why you would prefer one but still), you could try this:



                          while read key eq val; do
                          [ $key = "ValueTwo" ] && val=22222
                          printf "%s %s %sn" $key $eq $val
                          done < file





                          share|improve this answer












                          I'd go for awk:



                          awk '/ValueTwo/{$3=22222}1;' file > newfile


                          The above checks whether a given line matches ValueTwo and sets the 3d field to 222 on matching lines. The 1; is just an awk shgorthand way of writing print $0, it will print each line. Since it is outside the match block (/ValueTwo/{}), it will cause all lines to be printed.



                          Since you asked for a bash solution though (don't know why you would prefer one but still), you could try this:



                          while read key eq val; do
                          [ $key = "ValueTwo" ] && val=22222
                          printf "%s %s %sn" $key $eq $val
                          done < file






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jul 16 '14 at 14:11









                          terdon

                          128k31249423




                          128k31249423






























                              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.





                              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%2funix.stackexchange.com%2fquestions%2f144846%2fbash-one-liner-to-change-configuration-parameters%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

                              Accessing regular linux commands in Huawei's Dopra Linux

                              Can't connect RFCOMM socket: Host is down

                              Kernel panic - not syncing: Fatal Exception in Interrupt