How sed can be used to replace multiple patterns within a string for different patterns





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







0















I want to
(1) replace multiple numbers in a line for different values. For example, in the string "uncertainty = { 0.01 , 0.01 }" it should substitute for
string "uncertainty = { 0.2 , 0.2 }".
The problem is that
(2) these lines appear in hundreds of files with different quantities of numbers inside the brackets.
It could be
"uncertainty = { 0.1 , 0.1, 0.1 }", for "uncertainty = { 0.3 , 0.3, 0.3 }" or
"uncertainty = { 0.1 , 0.1, 0.1 , 0.1, 0.1, 0.1}", for
"uncertainty = { 0.3 , 0.3, 0.3 , 0.3, 0.3, 0.3}
and so on, and I have to substitute all of them.



I could not see how sed could be used to do that.










share|improve this question





























    0















    I want to
    (1) replace multiple numbers in a line for different values. For example, in the string "uncertainty = { 0.01 , 0.01 }" it should substitute for
    string "uncertainty = { 0.2 , 0.2 }".
    The problem is that
    (2) these lines appear in hundreds of files with different quantities of numbers inside the brackets.
    It could be
    "uncertainty = { 0.1 , 0.1, 0.1 }", for "uncertainty = { 0.3 , 0.3, 0.3 }" or
    "uncertainty = { 0.1 , 0.1, 0.1 , 0.1, 0.1, 0.1}", for
    "uncertainty = { 0.3 , 0.3, 0.3 , 0.3, 0.3, 0.3}
    and so on, and I have to substitute all of them.



    I could not see how sed could be used to do that.










    share|improve this question

























      0












      0








      0








      I want to
      (1) replace multiple numbers in a line for different values. For example, in the string "uncertainty = { 0.01 , 0.01 }" it should substitute for
      string "uncertainty = { 0.2 , 0.2 }".
      The problem is that
      (2) these lines appear in hundreds of files with different quantities of numbers inside the brackets.
      It could be
      "uncertainty = { 0.1 , 0.1, 0.1 }", for "uncertainty = { 0.3 , 0.3, 0.3 }" or
      "uncertainty = { 0.1 , 0.1, 0.1 , 0.1, 0.1, 0.1}", for
      "uncertainty = { 0.3 , 0.3, 0.3 , 0.3, 0.3, 0.3}
      and so on, and I have to substitute all of them.



      I could not see how sed could be used to do that.










      share|improve this question














      I want to
      (1) replace multiple numbers in a line for different values. For example, in the string "uncertainty = { 0.01 , 0.01 }" it should substitute for
      string "uncertainty = { 0.2 , 0.2 }".
      The problem is that
      (2) these lines appear in hundreds of files with different quantities of numbers inside the brackets.
      It could be
      "uncertainty = { 0.1 , 0.1, 0.1 }", for "uncertainty = { 0.3 , 0.3, 0.3 }" or
      "uncertainty = { 0.1 , 0.1, 0.1 , 0.1, 0.1, 0.1}", for
      "uncertainty = { 0.3 , 0.3, 0.3 , 0.3, 0.3, 0.3}
      and so on, and I have to substitute all of them.



      I could not see how sed could be used to do that.







      sed






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 14 at 2:35









      Edjard MotaEdjard Mota

      1




      1






















          2 Answers
          2






          active

          oldest

          votes


















          0














          Use regular expressions. Convert one item inside the curly brackets into a regular expression such as:



          [0-9].[0-9]1



          What this expression means is "a number, a period, a number, a one". If the format is consistent through-and-through, this will only replace instances of "0.01" when put through sed.Terminal screenshot



          There are other tools that would meet these needs as well, such as awk and tr, but you would still use regular expressions for them as well. To replace numbers such as this in bulk, you could replace that "1" with another [0-9] or a [1-3] to specify numbers in the range of 1-3.



          For more info on Regular Expressions see Rexegg. They greatly expand the usefulness of virtually all text manipulation utilities.






          share|improve this answer

































            0














            Again thanks a lot!! Your hint helped me find a solution.



            I read about awk and it turned out to help half of my questions: to go through substrings of a string that matches a given string. Because awk have some limitations, I used sed
            to clean up the mess, like this in which v1 and v2 are passed via a shell script that call this code



            The awk part
            {
            found = match($0,"uncertainty")
            i = 4
            if (found) {
            while ( i < NF){
            if ($i = v1) {
            $i = v2
            }
            i = i + 2
            }
            print " " $0
            }
            else
            {
            print $0
            }
            }



            The shell part



            !/bin/bash



            Check if directory already exists,



            !/bin/bash



            Check if directory already exists,



            bitSizeDir="$1"
            value1="$2"
            value2="$3"



            _mydir="$(pwd)"
            find $bitSizeDir/ -type f > Files;
            while read line; do
            folder=dirname "$line"
            file=basename "$line"
            if [ "$file" = "input.c" ] ; then
            awk -v v1=$value1 -v v2=$value2 -f changeValues.awk $line > input.new
            mv input.new $folder
            cd $folder
            rm $file
            mv input.new input.c
            sed -i '' "s/$3 , }/ } /g" input.c
            cd $_mydir
            fi
            done < Files
            rm Files






            share|improve this answer
























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "89"
              };
              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%2faskubuntu.com%2fquestions%2f1118091%2fhow-sed-can-be-used-to-replace-multiple-patterns-within-a-string-for-different-p%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









              0














              Use regular expressions. Convert one item inside the curly brackets into a regular expression such as:



              [0-9].[0-9]1



              What this expression means is "a number, a period, a number, a one". If the format is consistent through-and-through, this will only replace instances of "0.01" when put through sed.Terminal screenshot



              There are other tools that would meet these needs as well, such as awk and tr, but you would still use regular expressions for them as well. To replace numbers such as this in bulk, you could replace that "1" with another [0-9] or a [1-3] to specify numbers in the range of 1-3.



              For more info on Regular Expressions see Rexegg. They greatly expand the usefulness of virtually all text manipulation utilities.






              share|improve this answer






























                0














                Use regular expressions. Convert one item inside the curly brackets into a regular expression such as:



                [0-9].[0-9]1



                What this expression means is "a number, a period, a number, a one". If the format is consistent through-and-through, this will only replace instances of "0.01" when put through sed.Terminal screenshot



                There are other tools that would meet these needs as well, such as awk and tr, but you would still use regular expressions for them as well. To replace numbers such as this in bulk, you could replace that "1" with another [0-9] or a [1-3] to specify numbers in the range of 1-3.



                For more info on Regular Expressions see Rexegg. They greatly expand the usefulness of virtually all text manipulation utilities.






                share|improve this answer




























                  0












                  0








                  0







                  Use regular expressions. Convert one item inside the curly brackets into a regular expression such as:



                  [0-9].[0-9]1



                  What this expression means is "a number, a period, a number, a one". If the format is consistent through-and-through, this will only replace instances of "0.01" when put through sed.Terminal screenshot



                  There are other tools that would meet these needs as well, such as awk and tr, but you would still use regular expressions for them as well. To replace numbers such as this in bulk, you could replace that "1" with another [0-9] or a [1-3] to specify numbers in the range of 1-3.



                  For more info on Regular Expressions see Rexegg. They greatly expand the usefulness of virtually all text manipulation utilities.






                  share|improve this answer















                  Use regular expressions. Convert one item inside the curly brackets into a regular expression such as:



                  [0-9].[0-9]1



                  What this expression means is "a number, a period, a number, a one". If the format is consistent through-and-through, this will only replace instances of "0.01" when put through sed.Terminal screenshot



                  There are other tools that would meet these needs as well, such as awk and tr, but you would still use regular expressions for them as well. To replace numbers such as this in bulk, you could replace that "1" with another [0-9] or a [1-3] to specify numbers in the range of 1-3.



                  For more info on Regular Expressions see Rexegg. They greatly expand the usefulness of virtually all text manipulation utilities.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 14 at 3:27

























                  answered Feb 14 at 3:20









                  MintyMinty

                  89329




                  89329

























                      0














                      Again thanks a lot!! Your hint helped me find a solution.



                      I read about awk and it turned out to help half of my questions: to go through substrings of a string that matches a given string. Because awk have some limitations, I used sed
                      to clean up the mess, like this in which v1 and v2 are passed via a shell script that call this code



                      The awk part
                      {
                      found = match($0,"uncertainty")
                      i = 4
                      if (found) {
                      while ( i < NF){
                      if ($i = v1) {
                      $i = v2
                      }
                      i = i + 2
                      }
                      print " " $0
                      }
                      else
                      {
                      print $0
                      }
                      }



                      The shell part



                      !/bin/bash



                      Check if directory already exists,



                      !/bin/bash



                      Check if directory already exists,



                      bitSizeDir="$1"
                      value1="$2"
                      value2="$3"



                      _mydir="$(pwd)"
                      find $bitSizeDir/ -type f > Files;
                      while read line; do
                      folder=dirname "$line"
                      file=basename "$line"
                      if [ "$file" = "input.c" ] ; then
                      awk -v v1=$value1 -v v2=$value2 -f changeValues.awk $line > input.new
                      mv input.new $folder
                      cd $folder
                      rm $file
                      mv input.new input.c
                      sed -i '' "s/$3 , }/ } /g" input.c
                      cd $_mydir
                      fi
                      done < Files
                      rm Files






                      share|improve this answer




























                        0














                        Again thanks a lot!! Your hint helped me find a solution.



                        I read about awk and it turned out to help half of my questions: to go through substrings of a string that matches a given string. Because awk have some limitations, I used sed
                        to clean up the mess, like this in which v1 and v2 are passed via a shell script that call this code



                        The awk part
                        {
                        found = match($0,"uncertainty")
                        i = 4
                        if (found) {
                        while ( i < NF){
                        if ($i = v1) {
                        $i = v2
                        }
                        i = i + 2
                        }
                        print " " $0
                        }
                        else
                        {
                        print $0
                        }
                        }



                        The shell part



                        !/bin/bash



                        Check if directory already exists,



                        !/bin/bash



                        Check if directory already exists,



                        bitSizeDir="$1"
                        value1="$2"
                        value2="$3"



                        _mydir="$(pwd)"
                        find $bitSizeDir/ -type f > Files;
                        while read line; do
                        folder=dirname "$line"
                        file=basename "$line"
                        if [ "$file" = "input.c" ] ; then
                        awk -v v1=$value1 -v v2=$value2 -f changeValues.awk $line > input.new
                        mv input.new $folder
                        cd $folder
                        rm $file
                        mv input.new input.c
                        sed -i '' "s/$3 , }/ } /g" input.c
                        cd $_mydir
                        fi
                        done < Files
                        rm Files






                        share|improve this answer


























                          0












                          0








                          0







                          Again thanks a lot!! Your hint helped me find a solution.



                          I read about awk and it turned out to help half of my questions: to go through substrings of a string that matches a given string. Because awk have some limitations, I used sed
                          to clean up the mess, like this in which v1 and v2 are passed via a shell script that call this code



                          The awk part
                          {
                          found = match($0,"uncertainty")
                          i = 4
                          if (found) {
                          while ( i < NF){
                          if ($i = v1) {
                          $i = v2
                          }
                          i = i + 2
                          }
                          print " " $0
                          }
                          else
                          {
                          print $0
                          }
                          }



                          The shell part



                          !/bin/bash



                          Check if directory already exists,



                          !/bin/bash



                          Check if directory already exists,



                          bitSizeDir="$1"
                          value1="$2"
                          value2="$3"



                          _mydir="$(pwd)"
                          find $bitSizeDir/ -type f > Files;
                          while read line; do
                          folder=dirname "$line"
                          file=basename "$line"
                          if [ "$file" = "input.c" ] ; then
                          awk -v v1=$value1 -v v2=$value2 -f changeValues.awk $line > input.new
                          mv input.new $folder
                          cd $folder
                          rm $file
                          mv input.new input.c
                          sed -i '' "s/$3 , }/ } /g" input.c
                          cd $_mydir
                          fi
                          done < Files
                          rm Files






                          share|improve this answer













                          Again thanks a lot!! Your hint helped me find a solution.



                          I read about awk and it turned out to help half of my questions: to go through substrings of a string that matches a given string. Because awk have some limitations, I used sed
                          to clean up the mess, like this in which v1 and v2 are passed via a shell script that call this code



                          The awk part
                          {
                          found = match($0,"uncertainty")
                          i = 4
                          if (found) {
                          while ( i < NF){
                          if ($i = v1) {
                          $i = v2
                          }
                          i = i + 2
                          }
                          print " " $0
                          }
                          else
                          {
                          print $0
                          }
                          }



                          The shell part



                          !/bin/bash



                          Check if directory already exists,



                          !/bin/bash



                          Check if directory already exists,



                          bitSizeDir="$1"
                          value1="$2"
                          value2="$3"



                          _mydir="$(pwd)"
                          find $bitSizeDir/ -type f > Files;
                          while read line; do
                          folder=dirname "$line"
                          file=basename "$line"
                          if [ "$file" = "input.c" ] ; then
                          awk -v v1=$value1 -v v2=$value2 -f changeValues.awk $line > input.new
                          mv input.new $folder
                          cd $folder
                          rm $file
                          mv input.new input.c
                          sed -i '' "s/$3 , }/ } /g" input.c
                          cd $_mydir
                          fi
                          done < Files
                          rm Files







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Feb 17 at 20:18









                          Edjard MotaEdjard Mota

                          1




                          1






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Ask Ubuntu!


                              • 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%2faskubuntu.com%2fquestions%2f1118091%2fhow-sed-can-be-used-to-replace-multiple-patterns-within-a-string-for-different-p%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 change which sound is reproduced for terminal bell?

                              Can I use Tabulator js library in my java Spring + Thymeleaf project?

                              Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents