Replace a word every N lines with sed between a specific line interval












0














I have an input file:



Line 1 a
Line 2 b
Line 3 c
Line 4 d
Line 5 e
Line 6 f
Line 7 g
Line 8 h
Line 9 i
Line 10 j
Line 11 k
Line 12 l
Line 13 m
Line 14 n
Line 15 o
Line 16 p
Line 17 q
.
.
.


I want to insert with sed in a specific line interval, say between line 3 and line 17 of the file, a word that replaces the last word of each line every 4 lines.



In this case, let's say I want to put a Z in line 3 of the file, then line 7 of the file (i.e., 3+4), then line 11 of the file (i.e., 7+4), then line 15 of the file (i.e., 11+4).



Is there a way to do this with sed but just opening only once the file that I want to change?



The expected output would be:



Line 1 a
Line 2 b
Line 3 Z
Line 4 d
Line 5 e
Line 6 f
Line 7 Z
Line 8 h
Line 9 i
Line 10 j
Line 11 Z
Line 12 l
Line 13 m
Line 14 n
Line 15 Z
Line 16 p
Line 17 q
.
.
.









share|improve this question
























  • Generate the desired lines outside of sed: eg seq 46 44 180. Use that to generate the sed commands.
    – William Pursell
    Nov 21 '18 at 14:53
















0














I have an input file:



Line 1 a
Line 2 b
Line 3 c
Line 4 d
Line 5 e
Line 6 f
Line 7 g
Line 8 h
Line 9 i
Line 10 j
Line 11 k
Line 12 l
Line 13 m
Line 14 n
Line 15 o
Line 16 p
Line 17 q
.
.
.


I want to insert with sed in a specific line interval, say between line 3 and line 17 of the file, a word that replaces the last word of each line every 4 lines.



In this case, let's say I want to put a Z in line 3 of the file, then line 7 of the file (i.e., 3+4), then line 11 of the file (i.e., 7+4), then line 15 of the file (i.e., 11+4).



Is there a way to do this with sed but just opening only once the file that I want to change?



The expected output would be:



Line 1 a
Line 2 b
Line 3 Z
Line 4 d
Line 5 e
Line 6 f
Line 7 Z
Line 8 h
Line 9 i
Line 10 j
Line 11 Z
Line 12 l
Line 13 m
Line 14 n
Line 15 Z
Line 16 p
Line 17 q
.
.
.









share|improve this question
























  • Generate the desired lines outside of sed: eg seq 46 44 180. Use that to generate the sed commands.
    – William Pursell
    Nov 21 '18 at 14:53














0












0








0







I have an input file:



Line 1 a
Line 2 b
Line 3 c
Line 4 d
Line 5 e
Line 6 f
Line 7 g
Line 8 h
Line 9 i
Line 10 j
Line 11 k
Line 12 l
Line 13 m
Line 14 n
Line 15 o
Line 16 p
Line 17 q
.
.
.


I want to insert with sed in a specific line interval, say between line 3 and line 17 of the file, a word that replaces the last word of each line every 4 lines.



In this case, let's say I want to put a Z in line 3 of the file, then line 7 of the file (i.e., 3+4), then line 11 of the file (i.e., 7+4), then line 15 of the file (i.e., 11+4).



Is there a way to do this with sed but just opening only once the file that I want to change?



The expected output would be:



Line 1 a
Line 2 b
Line 3 Z
Line 4 d
Line 5 e
Line 6 f
Line 7 Z
Line 8 h
Line 9 i
Line 10 j
Line 11 Z
Line 12 l
Line 13 m
Line 14 n
Line 15 Z
Line 16 p
Line 17 q
.
.
.









share|improve this question















I have an input file:



Line 1 a
Line 2 b
Line 3 c
Line 4 d
Line 5 e
Line 6 f
Line 7 g
Line 8 h
Line 9 i
Line 10 j
Line 11 k
Line 12 l
Line 13 m
Line 14 n
Line 15 o
Line 16 p
Line 17 q
.
.
.


I want to insert with sed in a specific line interval, say between line 3 and line 17 of the file, a word that replaces the last word of each line every 4 lines.



In this case, let's say I want to put a Z in line 3 of the file, then line 7 of the file (i.e., 3+4), then line 11 of the file (i.e., 7+4), then line 15 of the file (i.e., 11+4).



Is there a way to do this with sed but just opening only once the file that I want to change?



The expected output would be:



Line 1 a
Line 2 b
Line 3 Z
Line 4 d
Line 5 e
Line 6 f
Line 7 Z
Line 8 h
Line 9 i
Line 10 j
Line 11 Z
Line 12 l
Line 13 m
Line 14 n
Line 15 Z
Line 16 p
Line 17 q
.
.
.






sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 15:12









Benjamin W.

20.3k134655




20.3k134655










asked Nov 21 '18 at 14:47









Ivan

53




53












  • Generate the desired lines outside of sed: eg seq 46 44 180. Use that to generate the sed commands.
    – William Pursell
    Nov 21 '18 at 14:53


















  • Generate the desired lines outside of sed: eg seq 46 44 180. Use that to generate the sed commands.
    – William Pursell
    Nov 21 '18 at 14:53
















Generate the desired lines outside of sed: eg seq 46 44 180. Use that to generate the sed commands.
– William Pursell
Nov 21 '18 at 14:53




Generate the desired lines outside of sed: eg seq 46 44 180. Use that to generate the sed commands.
– William Pursell
Nov 21 '18 at 14:53












2 Answers
2






active

oldest

votes


















2














If you have GNU sed, you can use the first~step line addressing form:



sed '3,17{3~4s/S*$/Z/}' infile


First, we limit all actions to an address range with 3,17{...}.



Then, within the curly braces, we run this:



3~4s/S*$/Z/


"On line 3 and every 4th line after, replace the last word of the line (S*$ – longest sequence of non-space characters) with Z".






share|improve this answer





























    0














    Using POSIX sed, you can do:



    sed '3,17{s/[^ ]*$/Z/;n;n;n;}'


    An alternative could be awk which can be made a bit more flexible:



    awk 'NR==3,NR==17{if (c++%4==0) { $NF="Z" }}1'





    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%2f53414602%2freplace-a-word-every-n-lines-with-sed-between-a-specific-line-interval%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









      2














      If you have GNU sed, you can use the first~step line addressing form:



      sed '3,17{3~4s/S*$/Z/}' infile


      First, we limit all actions to an address range with 3,17{...}.



      Then, within the curly braces, we run this:



      3~4s/S*$/Z/


      "On line 3 and every 4th line after, replace the last word of the line (S*$ – longest sequence of non-space characters) with Z".






      share|improve this answer


























        2














        If you have GNU sed, you can use the first~step line addressing form:



        sed '3,17{3~4s/S*$/Z/}' infile


        First, we limit all actions to an address range with 3,17{...}.



        Then, within the curly braces, we run this:



        3~4s/S*$/Z/


        "On line 3 and every 4th line after, replace the last word of the line (S*$ – longest sequence of non-space characters) with Z".






        share|improve this answer
























          2












          2








          2






          If you have GNU sed, you can use the first~step line addressing form:



          sed '3,17{3~4s/S*$/Z/}' infile


          First, we limit all actions to an address range with 3,17{...}.



          Then, within the curly braces, we run this:



          3~4s/S*$/Z/


          "On line 3 and every 4th line after, replace the last word of the line (S*$ – longest sequence of non-space characters) with Z".






          share|improve this answer












          If you have GNU sed, you can use the first~step line addressing form:



          sed '3,17{3~4s/S*$/Z/}' infile


          First, we limit all actions to an address range with 3,17{...}.



          Then, within the curly braces, we run this:



          3~4s/S*$/Z/


          "On line 3 and every 4th line after, replace the last word of the line (S*$ – longest sequence of non-space characters) with Z".







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 15:07









          Benjamin W.

          20.3k134655




          20.3k134655

























              0














              Using POSIX sed, you can do:



              sed '3,17{s/[^ ]*$/Z/;n;n;n;}'


              An alternative could be awk which can be made a bit more flexible:



              awk 'NR==3,NR==17{if (c++%4==0) { $NF="Z" }}1'





              share|improve this answer




























                0














                Using POSIX sed, you can do:



                sed '3,17{s/[^ ]*$/Z/;n;n;n;}'


                An alternative could be awk which can be made a bit more flexible:



                awk 'NR==3,NR==17{if (c++%4==0) { $NF="Z" }}1'





                share|improve this answer


























                  0












                  0








                  0






                  Using POSIX sed, you can do:



                  sed '3,17{s/[^ ]*$/Z/;n;n;n;}'


                  An alternative could be awk which can be made a bit more flexible:



                  awk 'NR==3,NR==17{if (c++%4==0) { $NF="Z" }}1'





                  share|improve this answer














                  Using POSIX sed, you can do:



                  sed '3,17{s/[^ ]*$/Z/;n;n;n;}'


                  An alternative could be awk which can be made a bit more flexible:



                  awk 'NR==3,NR==17{if (c++%4==0) { $NF="Z" }}1'






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 21 '18 at 18:51

























                  answered Nov 21 '18 at 18:40









                  kvantour

                  7,93831129




                  7,93831129






























                      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%2f53414602%2freplace-a-word-every-n-lines-with-sed-between-a-specific-line-interval%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

                      TypeError: fit_transform() missing 1 required positional argument: 'X'