replace a string with sed from specific lines












1















I do know how to replace a string from a specific line such as:



sed -i "<line number>s/<old string>/<new string>/g" <file name>


however I do not know how to replace a string from multiple lines for example from a line number 1 then 10 and then 100










share|improve this question

























  • sed doesn't support substitution by set of arbitrary numbers

    – RomanPerekhrest
    Dec 8 '18 at 21:33


















1















I do know how to replace a string from a specific line such as:



sed -i "<line number>s/<old string>/<new string>/g" <file name>


however I do not know how to replace a string from multiple lines for example from a line number 1 then 10 and then 100










share|improve this question

























  • sed doesn't support substitution by set of arbitrary numbers

    – RomanPerekhrest
    Dec 8 '18 at 21:33
















1












1








1


0






I do know how to replace a string from a specific line such as:



sed -i "<line number>s/<old string>/<new string>/g" <file name>


however I do not know how to replace a string from multiple lines for example from a line number 1 then 10 and then 100










share|improve this question
















I do know how to replace a string from a specific line such as:



sed -i "<line number>s/<old string>/<new string>/g" <file name>


however I do not know how to replace a string from multiple lines for example from a line number 1 then 10 and then 100







sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 8 '18 at 20:39









Jeff Schaller

39.7k1054126




39.7k1054126










asked Dec 8 '18 at 20:37









Dimitris MintisDimitris Mintis

805




805













  • sed doesn't support substitution by set of arbitrary numbers

    – RomanPerekhrest
    Dec 8 '18 at 21:33





















  • sed doesn't support substitution by set of arbitrary numbers

    – RomanPerekhrest
    Dec 8 '18 at 21:33



















sed doesn't support substitution by set of arbitrary numbers

– RomanPerekhrest
Dec 8 '18 at 21:33







sed doesn't support substitution by set of arbitrary numbers

– RomanPerekhrest
Dec 8 '18 at 21:33












4 Answers
4






active

oldest

votes


















2














Here is an awk answer.



awk 'NR == 1 || NR == 10 || NR == 100 {gsub(/old/,"new")}; {print}' <file name>


Explanation





  • NR == 1 || NR == 10 || NR == 100: only do the following commands on one of these lines.


  • gsub(/old/,"new"): substitute /old/ with new.


  • {print}: regardless of what line you are on, print the line.






share|improve this answer































    2














    As far as I know, sed addresses may only consist of a single line, or a range of lines.



    However you could cobble something together using sed -f - to read commands from standard input, together with your shell. For example:



      printf '%ds/<old string>/<new string>/gn' {1,10,100} | sed -f - file





    share|improve this answer
























    • Brilliant solution. Alternative to avoid the pipe: sed -f <(printf '%ds/line/LINE/gn' {1,10,100}) file1

      – George Vasiliou
      Dec 9 '18 at 17:36





















    1














    An alternative directly with sed:



     sed '1b1; 10b1; 100b1; b ;:1;s/<old string>/<new string>/g' <file name>


    If the line number matches either 1, 10, or 100, branch to label 1; on other lines, just branch to the end (which, by default, prints the line).



    somewhat automated:



     sed -e $(printf '%sb1;' 1 10 100) -e 'b; :1;s/<old>/<new>/g' <file name>


    POSIXly (with default IFS):



     sed $(printf -- '-e %sb1 ' 1 10 100) -e 'b' -e':1' -e 's/<old>/<new>/g'





    share|improve this answer

































      0














      Well, how about:



      sed -e '
      /something_identifying_startline/,/something_identifying_endline/{
      s/dog/cat/
      s/black/white/
      }





      share|improve this answer
























      • Welsome to U/L and thanks for posting an answer. However, this won't let you select multiple ranges (nor select by line number).

        – Sparhawk
        Dec 8 '18 at 22:03











      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%2f486840%2freplace-a-string-with-sed-from-specific-lines%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









      2














      Here is an awk answer.



      awk 'NR == 1 || NR == 10 || NR == 100 {gsub(/old/,"new")}; {print}' <file name>


      Explanation





      • NR == 1 || NR == 10 || NR == 100: only do the following commands on one of these lines.


      • gsub(/old/,"new"): substitute /old/ with new.


      • {print}: regardless of what line you are on, print the line.






      share|improve this answer




























        2














        Here is an awk answer.



        awk 'NR == 1 || NR == 10 || NR == 100 {gsub(/old/,"new")}; {print}' <file name>


        Explanation





        • NR == 1 || NR == 10 || NR == 100: only do the following commands on one of these lines.


        • gsub(/old/,"new"): substitute /old/ with new.


        • {print}: regardless of what line you are on, print the line.






        share|improve this answer


























          2












          2








          2







          Here is an awk answer.



          awk 'NR == 1 || NR == 10 || NR == 100 {gsub(/old/,"new")}; {print}' <file name>


          Explanation





          • NR == 1 || NR == 10 || NR == 100: only do the following commands on one of these lines.


          • gsub(/old/,"new"): substitute /old/ with new.


          • {print}: regardless of what line you are on, print the line.






          share|improve this answer













          Here is an awk answer.



          awk 'NR == 1 || NR == 10 || NR == 100 {gsub(/old/,"new")}; {print}' <file name>


          Explanation





          • NR == 1 || NR == 10 || NR == 100: only do the following commands on one of these lines.


          • gsub(/old/,"new"): substitute /old/ with new.


          • {print}: regardless of what line you are on, print the line.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 8 '18 at 22:06









          SparhawkSparhawk

          9,45763992




          9,45763992

























              2














              As far as I know, sed addresses may only consist of a single line, or a range of lines.



              However you could cobble something together using sed -f - to read commands from standard input, together with your shell. For example:



                printf '%ds/<old string>/<new string>/gn' {1,10,100} | sed -f - file





              share|improve this answer
























              • Brilliant solution. Alternative to avoid the pipe: sed -f <(printf '%ds/line/LINE/gn' {1,10,100}) file1

                – George Vasiliou
                Dec 9 '18 at 17:36


















              2














              As far as I know, sed addresses may only consist of a single line, or a range of lines.



              However you could cobble something together using sed -f - to read commands from standard input, together with your shell. For example:



                printf '%ds/<old string>/<new string>/gn' {1,10,100} | sed -f - file





              share|improve this answer
























              • Brilliant solution. Alternative to avoid the pipe: sed -f <(printf '%ds/line/LINE/gn' {1,10,100}) file1

                – George Vasiliou
                Dec 9 '18 at 17:36
















              2












              2








              2







              As far as I know, sed addresses may only consist of a single line, or a range of lines.



              However you could cobble something together using sed -f - to read commands from standard input, together with your shell. For example:



                printf '%ds/<old string>/<new string>/gn' {1,10,100} | sed -f - file





              share|improve this answer













              As far as I know, sed addresses may only consist of a single line, or a range of lines.



              However you could cobble something together using sed -f - to read commands from standard input, together with your shell. For example:



                printf '%ds/<old string>/<new string>/gn' {1,10,100} | sed -f - file






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 8 '18 at 23:16









              steeldriversteeldriver

              35.6k35286




              35.6k35286













              • Brilliant solution. Alternative to avoid the pipe: sed -f <(printf '%ds/line/LINE/gn' {1,10,100}) file1

                – George Vasiliou
                Dec 9 '18 at 17:36





















              • Brilliant solution. Alternative to avoid the pipe: sed -f <(printf '%ds/line/LINE/gn' {1,10,100}) file1

                – George Vasiliou
                Dec 9 '18 at 17:36



















              Brilliant solution. Alternative to avoid the pipe: sed -f <(printf '%ds/line/LINE/gn' {1,10,100}) file1

              – George Vasiliou
              Dec 9 '18 at 17:36







              Brilliant solution. Alternative to avoid the pipe: sed -f <(printf '%ds/line/LINE/gn' {1,10,100}) file1

              – George Vasiliou
              Dec 9 '18 at 17:36













              1














              An alternative directly with sed:



               sed '1b1; 10b1; 100b1; b ;:1;s/<old string>/<new string>/g' <file name>


              If the line number matches either 1, 10, or 100, branch to label 1; on other lines, just branch to the end (which, by default, prints the line).



              somewhat automated:



               sed -e $(printf '%sb1;' 1 10 100) -e 'b; :1;s/<old>/<new>/g' <file name>


              POSIXly (with default IFS):



               sed $(printf -- '-e %sb1 ' 1 10 100) -e 'b' -e':1' -e 's/<old>/<new>/g'





              share|improve this answer






























                1














                An alternative directly with sed:



                 sed '1b1; 10b1; 100b1; b ;:1;s/<old string>/<new string>/g' <file name>


                If the line number matches either 1, 10, or 100, branch to label 1; on other lines, just branch to the end (which, by default, prints the line).



                somewhat automated:



                 sed -e $(printf '%sb1;' 1 10 100) -e 'b; :1;s/<old>/<new>/g' <file name>


                POSIXly (with default IFS):



                 sed $(printf -- '-e %sb1 ' 1 10 100) -e 'b' -e':1' -e 's/<old>/<new>/g'





                share|improve this answer




























                  1












                  1








                  1







                  An alternative directly with sed:



                   sed '1b1; 10b1; 100b1; b ;:1;s/<old string>/<new string>/g' <file name>


                  If the line number matches either 1, 10, or 100, branch to label 1; on other lines, just branch to the end (which, by default, prints the line).



                  somewhat automated:



                   sed -e $(printf '%sb1;' 1 10 100) -e 'b; :1;s/<old>/<new>/g' <file name>


                  POSIXly (with default IFS):



                   sed $(printf -- '-e %sb1 ' 1 10 100) -e 'b' -e':1' -e 's/<old>/<new>/g'





                  share|improve this answer















                  An alternative directly with sed:



                   sed '1b1; 10b1; 100b1; b ;:1;s/<old string>/<new string>/g' <file name>


                  If the line number matches either 1, 10, or 100, branch to label 1; on other lines, just branch to the end (which, by default, prints the line).



                  somewhat automated:



                   sed -e $(printf '%sb1;' 1 10 100) -e 'b; :1;s/<old>/<new>/g' <file name>


                  POSIXly (with default IFS):



                   sed $(printf -- '-e %sb1 ' 1 10 100) -e 'b' -e':1' -e 's/<old>/<new>/g'






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 9 '18 at 1:05









                  Jeff Schaller

                  39.7k1054126




                  39.7k1054126










                  answered Dec 9 '18 at 0:30









                  IsaacIsaac

                  11.6k11652




                  11.6k11652























                      0














                      Well, how about:



                      sed -e '
                      /something_identifying_startline/,/something_identifying_endline/{
                      s/dog/cat/
                      s/black/white/
                      }





                      share|improve this answer
























                      • Welsome to U/L and thanks for posting an answer. However, this won't let you select multiple ranges (nor select by line number).

                        – Sparhawk
                        Dec 8 '18 at 22:03
















                      0














                      Well, how about:



                      sed -e '
                      /something_identifying_startline/,/something_identifying_endline/{
                      s/dog/cat/
                      s/black/white/
                      }





                      share|improve this answer
























                      • Welsome to U/L and thanks for posting an answer. However, this won't let you select multiple ranges (nor select by line number).

                        – Sparhawk
                        Dec 8 '18 at 22:03














                      0












                      0








                      0







                      Well, how about:



                      sed -e '
                      /something_identifying_startline/,/something_identifying_endline/{
                      s/dog/cat/
                      s/black/white/
                      }





                      share|improve this answer













                      Well, how about:



                      sed -e '
                      /something_identifying_startline/,/something_identifying_endline/{
                      s/dog/cat/
                      s/black/white/
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 8 '18 at 22:00









                      Don WDon W

                      1




                      1













                      • Welsome to U/L and thanks for posting an answer. However, this won't let you select multiple ranges (nor select by line number).

                        – Sparhawk
                        Dec 8 '18 at 22:03



















                      • Welsome to U/L and thanks for posting an answer. However, this won't let you select multiple ranges (nor select by line number).

                        – Sparhawk
                        Dec 8 '18 at 22:03

















                      Welsome to U/L and thanks for posting an answer. However, this won't let you select multiple ranges (nor select by line number).

                      – Sparhawk
                      Dec 8 '18 at 22:03





                      Welsome to U/L and thanks for posting an answer. However, this won't let you select multiple ranges (nor select by line number).

                      – Sparhawk
                      Dec 8 '18 at 22:03


















                      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%2f486840%2freplace-a-string-with-sed-from-specific-lines%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

                      Berounka

                      Sphinx de Gizeh

                      Different font size/position of beamer's navigation symbols template's content depending on regular/plain...