Replace block of text with sed, awk, perl or vim












0















I am trying to replace every instance of the following block of six rows (meaning that those characters are repeated in many places in the same columns of a big text file, called a.txt)



xx\
xx\
xx\
xx\
xx\
xx\


with this block of six rows, just reducing the six rows block of two columns to a six rows block of only one column



x
x
x
x
x
x


I have tried the following code (seen on unix.stackexchange 1) without success:



sed -i '/xx/,/xx/,/xx/,/xx/,/xx/,/xx/c
x
x
x
x
x
x' a.txt


I have had no better results with awk or vim or perl.










share|improve this question









New contributor




ocap is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2





    You mention in your first sentence that the file may have several columns, and that what you show is just one column out of many. Is that so?

    – Kusalananda
    6 hours ago






  • 2





    Is that lines consisting literally of the two letters x and two backslashes? I'm not sure how that is two columns, can you edit the question to clarify? And if it's not literally xx<backslash><backslash>, can you add an example of what the data actually would be?

    – ilkkachu
    6 hours ago


















0















I am trying to replace every instance of the following block of six rows (meaning that those characters are repeated in many places in the same columns of a big text file, called a.txt)



xx\
xx\
xx\
xx\
xx\
xx\


with this block of six rows, just reducing the six rows block of two columns to a six rows block of only one column



x
x
x
x
x
x


I have tried the following code (seen on unix.stackexchange 1) without success:



sed -i '/xx/,/xx/,/xx/,/xx/,/xx/,/xx/c
x
x
x
x
x
x' a.txt


I have had no better results with awk or vim or perl.










share|improve this question









New contributor




ocap is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2





    You mention in your first sentence that the file may have several columns, and that what you show is just one column out of many. Is that so?

    – Kusalananda
    6 hours ago






  • 2





    Is that lines consisting literally of the two letters x and two backslashes? I'm not sure how that is two columns, can you edit the question to clarify? And if it's not literally xx<backslash><backslash>, can you add an example of what the data actually would be?

    – ilkkachu
    6 hours ago
















0












0








0


0






I am trying to replace every instance of the following block of six rows (meaning that those characters are repeated in many places in the same columns of a big text file, called a.txt)



xx\
xx\
xx\
xx\
xx\
xx\


with this block of six rows, just reducing the six rows block of two columns to a six rows block of only one column



x
x
x
x
x
x


I have tried the following code (seen on unix.stackexchange 1) without success:



sed -i '/xx/,/xx/,/xx/,/xx/,/xx/,/xx/c
x
x
x
x
x
x' a.txt


I have had no better results with awk or vim or perl.










share|improve this question









New contributor




ocap is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I am trying to replace every instance of the following block of six rows (meaning that those characters are repeated in many places in the same columns of a big text file, called a.txt)



xx\
xx\
xx\
xx\
xx\
xx\


with this block of six rows, just reducing the six rows block of two columns to a six rows block of only one column



x
x
x
x
x
x


I have tried the following code (seen on unix.stackexchange 1) without success:



sed -i '/xx/,/xx/,/xx/,/xx/,/xx/,/xx/c
x
x
x
x
x
x' a.txt


I have had no better results with awk or vim or perl.







awk sed vim perl






share|improve this question









New contributor




ocap is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




ocap is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 5 hours ago









Rui F Ribeiro

41.4k1481140




41.4k1481140






New contributor




ocap is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 7 hours ago









ocapocap

1




1




New contributor




ocap is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





ocap is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






ocap is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2





    You mention in your first sentence that the file may have several columns, and that what you show is just one column out of many. Is that so?

    – Kusalananda
    6 hours ago






  • 2





    Is that lines consisting literally of the two letters x and two backslashes? I'm not sure how that is two columns, can you edit the question to clarify? And if it's not literally xx<backslash><backslash>, can you add an example of what the data actually would be?

    – ilkkachu
    6 hours ago
















  • 2





    You mention in your first sentence that the file may have several columns, and that what you show is just one column out of many. Is that so?

    – Kusalananda
    6 hours ago






  • 2





    Is that lines consisting literally of the two letters x and two backslashes? I'm not sure how that is two columns, can you edit the question to clarify? And if it's not literally xx<backslash><backslash>, can you add an example of what the data actually would be?

    – ilkkachu
    6 hours ago










2




2





You mention in your first sentence that the file may have several columns, and that what you show is just one column out of many. Is that so?

– Kusalananda
6 hours ago





You mention in your first sentence that the file may have several columns, and that what you show is just one column out of many. Is that so?

– Kusalananda
6 hours ago




2




2





Is that lines consisting literally of the two letters x and two backslashes? I'm not sure how that is two columns, can you edit the question to clarify? And if it's not literally xx<backslash><backslash>, can you add an example of what the data actually would be?

– ilkkachu
6 hours ago







Is that lines consisting literally of the two letters x and two backslashes? I'm not sure how that is two columns, can you edit the question to clarify? And if it's not literally xx<backslash><backslash>, can you add an example of what the data actually would be?

– ilkkachu
6 hours ago












2 Answers
2






active

oldest

votes


















1














I would just go with Perl, and slurp the file in to do a single regex substitution on it. With the blocks consisting of the literal string xx\ (two letters ex, two backslashes), replaced with x:



perl -0777 -pe 's/(^xx\\n){6}/"x\n" x 6/meg' < file


^xx\\n is simply a start of a line, xx, two escaped backslashes, and a newline. {6} demands the text in parenthesis to repeat six times. In the replacement part we have x, backslash, a newline, and that string is "multiplied" by six. (/m makes ^ match start of line too, /e makes the replacement a Perl expression instead of just a string, and /g makes a global replacement, replacing all occurrences.)






share|improve this answer































    0














    You can set your block as the record separator, and then for each line chomp it and append the replacement block. Notice that the file may not end with such a block (just as it may not end with a newline).



    perl -pe 'BEGIN{$/ = "xx\\n" x 6; $r = "x\n" x 6} $_ .= $r if chomp' file


    Of course, you can just slurp the whole file, and with small files that's the fastest way out, but that's terrible as an example, because it precludes demonstrating scalable approaches and methods that could be used with any file, no matter how big.






    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
      });


      }
      });






      ocap is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505511%2freplace-block-of-text-with-sed-awk-perl-or-vim%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














      I would just go with Perl, and slurp the file in to do a single regex substitution on it. With the blocks consisting of the literal string xx\ (two letters ex, two backslashes), replaced with x:



      perl -0777 -pe 's/(^xx\\n){6}/"x\n" x 6/meg' < file


      ^xx\\n is simply a start of a line, xx, two escaped backslashes, and a newline. {6} demands the text in parenthesis to repeat six times. In the replacement part we have x, backslash, a newline, and that string is "multiplied" by six. (/m makes ^ match start of line too, /e makes the replacement a Perl expression instead of just a string, and /g makes a global replacement, replacing all occurrences.)






      share|improve this answer




























        1














        I would just go with Perl, and slurp the file in to do a single regex substitution on it. With the blocks consisting of the literal string xx\ (two letters ex, two backslashes), replaced with x:



        perl -0777 -pe 's/(^xx\\n){6}/"x\n" x 6/meg' < file


        ^xx\\n is simply a start of a line, xx, two escaped backslashes, and a newline. {6} demands the text in parenthesis to repeat six times. In the replacement part we have x, backslash, a newline, and that string is "multiplied" by six. (/m makes ^ match start of line too, /e makes the replacement a Perl expression instead of just a string, and /g makes a global replacement, replacing all occurrences.)






        share|improve this answer


























          1












          1








          1







          I would just go with Perl, and slurp the file in to do a single regex substitution on it. With the blocks consisting of the literal string xx\ (two letters ex, two backslashes), replaced with x:



          perl -0777 -pe 's/(^xx\\n){6}/"x\n" x 6/meg' < file


          ^xx\\n is simply a start of a line, xx, two escaped backslashes, and a newline. {6} demands the text in parenthesis to repeat six times. In the replacement part we have x, backslash, a newline, and that string is "multiplied" by six. (/m makes ^ match start of line too, /e makes the replacement a Perl expression instead of just a string, and /g makes a global replacement, replacing all occurrences.)






          share|improve this answer













          I would just go with Perl, and slurp the file in to do a single regex substitution on it. With the blocks consisting of the literal string xx\ (two letters ex, two backslashes), replaced with x:



          perl -0777 -pe 's/(^xx\\n){6}/"x\n" x 6/meg' < file


          ^xx\\n is simply a start of a line, xx, two escaped backslashes, and a newline. {6} demands the text in parenthesis to repeat six times. In the replacement part we have x, backslash, a newline, and that string is "multiplied" by six. (/m makes ^ match start of line too, /e makes the replacement a Perl expression instead of just a string, and /g makes a global replacement, replacing all occurrences.)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 6 hours ago









          ilkkachuilkkachu

          60.7k1098172




          60.7k1098172

























              0














              You can set your block as the record separator, and then for each line chomp it and append the replacement block. Notice that the file may not end with such a block (just as it may not end with a newline).



              perl -pe 'BEGIN{$/ = "xx\\n" x 6; $r = "x\n" x 6} $_ .= $r if chomp' file


              Of course, you can just slurp the whole file, and with small files that's the fastest way out, but that's terrible as an example, because it precludes demonstrating scalable approaches and methods that could be used with any file, no matter how big.






              share|improve this answer






























                0














                You can set your block as the record separator, and then for each line chomp it and append the replacement block. Notice that the file may not end with such a block (just as it may not end with a newline).



                perl -pe 'BEGIN{$/ = "xx\\n" x 6; $r = "x\n" x 6} $_ .= $r if chomp' file


                Of course, you can just slurp the whole file, and with small files that's the fastest way out, but that's terrible as an example, because it precludes demonstrating scalable approaches and methods that could be used with any file, no matter how big.






                share|improve this answer




























                  0












                  0








                  0







                  You can set your block as the record separator, and then for each line chomp it and append the replacement block. Notice that the file may not end with such a block (just as it may not end with a newline).



                  perl -pe 'BEGIN{$/ = "xx\\n" x 6; $r = "x\n" x 6} $_ .= $r if chomp' file


                  Of course, you can just slurp the whole file, and with small files that's the fastest way out, but that's terrible as an example, because it precludes demonstrating scalable approaches and methods that could be used with any file, no matter how big.






                  share|improve this answer















                  You can set your block as the record separator, and then for each line chomp it and append the replacement block. Notice that the file may not end with such a block (just as it may not end with a newline).



                  perl -pe 'BEGIN{$/ = "xx\\n" x 6; $r = "x\n" x 6} $_ .= $r if chomp' file


                  Of course, you can just slurp the whole file, and with small files that's the fastest way out, but that's terrible as an example, because it precludes demonstrating scalable approaches and methods that could be used with any file, no matter how big.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 4 hours ago

























                  answered 4 hours ago









                  Uncle BillyUncle Billy

                  6476




                  6476






















                      ocap is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      ocap is a new contributor. Be nice, and check out our Code of Conduct.













                      ocap is a new contributor. Be nice, and check out our Code of Conduct.












                      ocap is a new contributor. Be nice, and check out our Code of Conduct.
















                      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%2f505511%2freplace-block-of-text-with-sed-awk-perl-or-vim%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

                      Loup dans la culture

                      How to solve the problem of ntp “Unable to contact time server” from KDE?

                      Connection limited (no internet access)