How can I replace end line with fixed text when the next line begins with a defined set of characters?











up vote
2
down vote

favorite












I have several big files with some measurements.



It looks this way:



N 12344;PE 9.9999999;...
#S 0 0 31 44 75 130 165 196...
#S_+ "2 5 2 3 3 1 1 2 3 1 2 2...

N 12345;PE 9.9999999;...
#S 0 0 34 57 84 133 152...
#S_+ "1 0 1 1 2 3 0 0 0...

N 12346;PE 9.9999999;...
#S 0 0 31 44 73 140 169...
#S_+ "3 3 4 0 0 2 1 2 4...

N 25104;PE 9.9999999;...
#S 0 0 36 52 102 108 145...
#S_+ "1 1 0 1 0 0 3 0 1...

N 25105;PE 9.9999999;...
#S 0 0 32 58 88 130 143...


Sample is here:
http://pasted.co/d9806b7c4



The file is much bigger but I replaced part of the data with "..." to make it shorter.



I need to somehow replace the line ends before "#S" - in fact simply merge the "N" line with the following two ones into one line (or with the following three ones so I can get rid of the blank lines). Expect output like this:



N 12344;PE 9.9999999; #S 0 0 31 44 75 130 165 196 #S_+ "2 5 2 3 3 1 1 2 3 1 2 2...
N 12345;PE 9.9999999; #S 0 0 34 57 84 133 152 #S_+ "1 0 1 1 2 3 0 0 0...
N 12346;PE 9.9999999; #S 0 0 31 44 73 140 169 #S_+ "3 3 4 0 0 2 1 2 4...
N 25104;PE 9.9999999; #S 0 0 36 52 102 108 145 #S_+ "1 1 0 1 0 0 3 0 1...
N 25105;PE 9.9999999; #S 0 0 32 58 88 130 143...


Is this possible to achieve using some command-line utility in linux?



My knowledge is quite limited in this area so I would appreciate any help.



thanks










share|improve this question
























  • thanks to grawity for helping me with the code :-)
    – Juhele
    16 hours ago






  • 1




    @Pimp Juice IT: OK, I updated the question.
    – Juhele
    15 hours ago















up vote
2
down vote

favorite












I have several big files with some measurements.



It looks this way:



N 12344;PE 9.9999999;...
#S 0 0 31 44 75 130 165 196...
#S_+ "2 5 2 3 3 1 1 2 3 1 2 2...

N 12345;PE 9.9999999;...
#S 0 0 34 57 84 133 152...
#S_+ "1 0 1 1 2 3 0 0 0...

N 12346;PE 9.9999999;...
#S 0 0 31 44 73 140 169...
#S_+ "3 3 4 0 0 2 1 2 4...

N 25104;PE 9.9999999;...
#S 0 0 36 52 102 108 145...
#S_+ "1 1 0 1 0 0 3 0 1...

N 25105;PE 9.9999999;...
#S 0 0 32 58 88 130 143...


Sample is here:
http://pasted.co/d9806b7c4



The file is much bigger but I replaced part of the data with "..." to make it shorter.



I need to somehow replace the line ends before "#S" - in fact simply merge the "N" line with the following two ones into one line (or with the following three ones so I can get rid of the blank lines). Expect output like this:



N 12344;PE 9.9999999; #S 0 0 31 44 75 130 165 196 #S_+ "2 5 2 3 3 1 1 2 3 1 2 2...
N 12345;PE 9.9999999; #S 0 0 34 57 84 133 152 #S_+ "1 0 1 1 2 3 0 0 0...
N 12346;PE 9.9999999; #S 0 0 31 44 73 140 169 #S_+ "3 3 4 0 0 2 1 2 4...
N 25104;PE 9.9999999; #S 0 0 36 52 102 108 145 #S_+ "1 1 0 1 0 0 3 0 1...
N 25105;PE 9.9999999; #S 0 0 32 58 88 130 143...


Is this possible to achieve using some command-line utility in linux?



My knowledge is quite limited in this area so I would appreciate any help.



thanks










share|improve this question
























  • thanks to grawity for helping me with the code :-)
    – Juhele
    16 hours ago






  • 1




    @Pimp Juice IT: OK, I updated the question.
    – Juhele
    15 hours ago













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have several big files with some measurements.



It looks this way:



N 12344;PE 9.9999999;...
#S 0 0 31 44 75 130 165 196...
#S_+ "2 5 2 3 3 1 1 2 3 1 2 2...

N 12345;PE 9.9999999;...
#S 0 0 34 57 84 133 152...
#S_+ "1 0 1 1 2 3 0 0 0...

N 12346;PE 9.9999999;...
#S 0 0 31 44 73 140 169...
#S_+ "3 3 4 0 0 2 1 2 4...

N 25104;PE 9.9999999;...
#S 0 0 36 52 102 108 145...
#S_+ "1 1 0 1 0 0 3 0 1...

N 25105;PE 9.9999999;...
#S 0 0 32 58 88 130 143...


Sample is here:
http://pasted.co/d9806b7c4



The file is much bigger but I replaced part of the data with "..." to make it shorter.



I need to somehow replace the line ends before "#S" - in fact simply merge the "N" line with the following two ones into one line (or with the following three ones so I can get rid of the blank lines). Expect output like this:



N 12344;PE 9.9999999; #S 0 0 31 44 75 130 165 196 #S_+ "2 5 2 3 3 1 1 2 3 1 2 2...
N 12345;PE 9.9999999; #S 0 0 34 57 84 133 152 #S_+ "1 0 1 1 2 3 0 0 0...
N 12346;PE 9.9999999; #S 0 0 31 44 73 140 169 #S_+ "3 3 4 0 0 2 1 2 4...
N 25104;PE 9.9999999; #S 0 0 36 52 102 108 145 #S_+ "1 1 0 1 0 0 3 0 1...
N 25105;PE 9.9999999; #S 0 0 32 58 88 130 143...


Is this possible to achieve using some command-line utility in linux?



My knowledge is quite limited in this area so I would appreciate any help.



thanks










share|improve this question















I have several big files with some measurements.



It looks this way:



N 12344;PE 9.9999999;...
#S 0 0 31 44 75 130 165 196...
#S_+ "2 5 2 3 3 1 1 2 3 1 2 2...

N 12345;PE 9.9999999;...
#S 0 0 34 57 84 133 152...
#S_+ "1 0 1 1 2 3 0 0 0...

N 12346;PE 9.9999999;...
#S 0 0 31 44 73 140 169...
#S_+ "3 3 4 0 0 2 1 2 4...

N 25104;PE 9.9999999;...
#S 0 0 36 52 102 108 145...
#S_+ "1 1 0 1 0 0 3 0 1...

N 25105;PE 9.9999999;...
#S 0 0 32 58 88 130 143...


Sample is here:
http://pasted.co/d9806b7c4



The file is much bigger but I replaced part of the data with "..." to make it shorter.



I need to somehow replace the line ends before "#S" - in fact simply merge the "N" line with the following two ones into one line (or with the following three ones so I can get rid of the blank lines). Expect output like this:



N 12344;PE 9.9999999; #S 0 0 31 44 75 130 165 196 #S_+ "2 5 2 3 3 1 1 2 3 1 2 2...
N 12345;PE 9.9999999; #S 0 0 34 57 84 133 152 #S_+ "1 0 1 1 2 3 0 0 0...
N 12346;PE 9.9999999; #S 0 0 31 44 73 140 169 #S_+ "3 3 4 0 0 2 1 2 4...
N 25104;PE 9.9999999; #S 0 0 36 52 102 108 145 #S_+ "1 1 0 1 0 0 3 0 1...
N 25105;PE 9.9999999; #S 0 0 32 58 88 130 143...


Is this possible to achieve using some command-line utility in linux?



My knowledge is quite limited in this area so I would appreciate any help.



thanks







linux command-line regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 16 hours ago

























asked 16 hours ago









Juhele

2,07221222




2,07221222












  • thanks to grawity for helping me with the code :-)
    – Juhele
    16 hours ago






  • 1




    @Pimp Juice IT: OK, I updated the question.
    – Juhele
    15 hours ago


















  • thanks to grawity for helping me with the code :-)
    – Juhele
    16 hours ago






  • 1




    @Pimp Juice IT: OK, I updated the question.
    – Juhele
    15 hours ago
















thanks to grawity for helping me with the code :-)
– Juhele
16 hours ago




thanks to grawity for helping me with the code :-)
– Juhele
16 hours ago




1




1




@Pimp Juice IT: OK, I updated the question.
– Juhele
15 hours ago




@Pimp Juice IT: OK, I updated the question.
– Juhele
15 hours ago










6 Answers
6






active

oldest

votes

















up vote
4
down vote













With sed:



sed -z -e 's/n#S/ #S/g' -e 's/nN /N /g' data


In slow-mo:





  • -z makes sed consider the file as a single line (so the line ends are plain characters)


  • 's/n#S/#S/g' replaces all LF's occurring just before a #S by a space


  • -e 's/nN /N /g' replaces all LFs before N (ie, the blank lines)






share|improve this answer




























    up vote
    4
    down vote













    This is a portable solution with POSIX sed, implementing the following rules:




    • empty lines shall be deleted;

    • any line starting with #S shall be merged with the previous non-empty line, with a single space character between them, unless there is no previous non-empty line.


    The code:



    <data sed '/^$/ d; :start; N; s/n$//; t start; s/n#S/ #S/; t start; P; D'


    The same with comments (still working code):



    <data sed '
    /^$/ d # If empty line read, delete it and start a new cycle.
    :start # A label.
    N # Read additional line, there are now two lines in the pattern space.
    s/n$// # If the second line is empty, replace the newline with nothing.
    t start # If the above replacement occurred, go to start (to add another line).
    # Otherwise
    s/n#S/ #S/ # if the second line starts with #S, replace the newline with space.
    t start # If the above replacement occurred, go to start (to add another line).
    # Otherwise
    # (i.e when non-empty line not starting with #S occurred)
    P # print the pattern space up to the first newline and...
    D # delete the initial segment of the pattern space
    # through the first newline (i.e. everything just printed),
    # and start the next cycle with the resultant pattern space
    # and without reading any new input
    # (in our case the new input will be explicitly read by N then).
    '





    share|improve this answer






























      up vote
      3
      down vote













      With paste (this requires to always have groups of 4 lines):



       paste -s -d '   n' data


      In slo-mo:





      • paste -s concatenates the lines from the file


      • -d specifies characters to be inserted as delimiters. When there are several characters, they are used in a round-robin fashion, so with 3 spaces and a LF:


        • the first space is used on the first splice (N to #S),

        • the second space is used on the second splice (#S to #S),

        • the third space is used on the thrid splice (#S to blank line),

        • the last delimiter, a LF, is used on the fourth splice (blank line to N)

        • and the cycle repeats for the next 4 lines.








      share|improve this answer




























        up vote
        3
        down vote













        Using Perl:



        perl -0 -ape 's/R(?=RN|#)/ /g' file.txt
        N 12344;PE 9.9999999;... #S 0 0 31 44 75 130 165 196... #S_+ "2 5 2 3 3 1 1 2 3 1 2 2...
        N 12345;PE 9.9999999;... #S 0 0 34 57 84 133 152... #S_+ "1 0 1 1 2 3 0 0 0...
        N 12346;PE 9.9999999;... #S 0 0 31 44 73 140 169... #S_+ "3 3 4 0 0 2 1 2 4...
        N 25104;PE 9.9999999;... #S 0 0 36 52 102 108 145... #S_+ "1 1 0 1 0 0 3 0 1...
        N 25105;PE 9.9999999;... #S 0 0 32 58 88 130 143...


        Regex explain:



        s/              : substitute
        R : any kind of line break (ie. r, n, rn)
        (?= : positive lookahead, zero-length assertion that make sure we have after
        RN : a line break followed by letter N
        | : OR
        # : # character
        ) : end lookahead
        / /g : replace with a space, global





        share|improve this answer























        • @KamilMaciorowski: Yes, you're right. Fixed
          – Toto
          13 hours ago


















        up vote
        1
        down vote













        awk (gawk)



        As usually other than sed you can use awk (and in many different ways...)



        awk 'ORS=" "; NR % 4 == 0 && ORS="n" ' data


        where





        • ORS=" " fixes the output record separator, by default a newline, to a space (you can change)


        • NR % 4 == 0 && ORS="n" each 4th line it fixes back to the newline n

        • If nothing else is specified awk prints the full line


        • data is your data file.


        If you want you can use regex as in sed (in a similar way).






        share|improve this answer




























          up vote
          0
          down vote













          You can do it with any text editor that support regular expressions like Notepad++.



          The new line is just simple non-printable character or two characters. In Windows usually CarrigeReturn and LineFeed and in Unix based system usually LineFeed only.



          To see them you need to turn on showing non-printable character (usually a Paragraph icon)
          See here: https://imgur.com/cqiTvrp



          Now what you need to do is to use regular expression replacer (CTRL + H) to replace CRLF#S to #S.
          The symbol for CR is r and for LF is n. So you gonna end up with rn#S or n#S to #S.
          https://imgur.com/GoeVn70



          Or you can replace it to SPACE if you need.






          share|improve this answer








          New contributor




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


















          • The question is tagged "Linux"....
            – xenoid
            15 hours ago










          • I think regular expressions in Geany are the same. Is used Notepad++ as an example beacuse I am currently at Windows.
            – KaRolthas
            15 hours ago












          • The question also asks for a command-line utility...
            – xenoid
            15 hours ago










          • Nice, works. I need to somehow process at least few files now so even Notepad++ helps when I am working on my other machine with Windows. thanks
            – Juhele
            15 hours ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          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',
          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%2fsuperuser.com%2fquestions%2f1377291%2fhow-can-i-replace-end-line-with-fixed-text-when-the-next-line-begins-with-a-defi%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote













          With sed:



          sed -z -e 's/n#S/ #S/g' -e 's/nN /N /g' data


          In slow-mo:





          • -z makes sed consider the file as a single line (so the line ends are plain characters)


          • 's/n#S/#S/g' replaces all LF's occurring just before a #S by a space


          • -e 's/nN /N /g' replaces all LFs before N (ie, the blank lines)






          share|improve this answer

























            up vote
            4
            down vote













            With sed:



            sed -z -e 's/n#S/ #S/g' -e 's/nN /N /g' data


            In slow-mo:





            • -z makes sed consider the file as a single line (so the line ends are plain characters)


            • 's/n#S/#S/g' replaces all LF's occurring just before a #S by a space


            • -e 's/nN /N /g' replaces all LFs before N (ie, the blank lines)






            share|improve this answer























              up vote
              4
              down vote










              up vote
              4
              down vote









              With sed:



              sed -z -e 's/n#S/ #S/g' -e 's/nN /N /g' data


              In slow-mo:





              • -z makes sed consider the file as a single line (so the line ends are plain characters)


              • 's/n#S/#S/g' replaces all LF's occurring just before a #S by a space


              • -e 's/nN /N /g' replaces all LFs before N (ie, the blank lines)






              share|improve this answer












              With sed:



              sed -z -e 's/n#S/ #S/g' -e 's/nN /N /g' data


              In slow-mo:





              • -z makes sed consider the file as a single line (so the line ends are plain characters)


              • 's/n#S/#S/g' replaces all LF's occurring just before a #S by a space


              • -e 's/nN /N /g' replaces all LFs before N (ie, the blank lines)







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 15 hours ago









              xenoid

              3,4973618




              3,4973618
























                  up vote
                  4
                  down vote













                  This is a portable solution with POSIX sed, implementing the following rules:




                  • empty lines shall be deleted;

                  • any line starting with #S shall be merged with the previous non-empty line, with a single space character between them, unless there is no previous non-empty line.


                  The code:



                  <data sed '/^$/ d; :start; N; s/n$//; t start; s/n#S/ #S/; t start; P; D'


                  The same with comments (still working code):



                  <data sed '
                  /^$/ d # If empty line read, delete it and start a new cycle.
                  :start # A label.
                  N # Read additional line, there are now two lines in the pattern space.
                  s/n$// # If the second line is empty, replace the newline with nothing.
                  t start # If the above replacement occurred, go to start (to add another line).
                  # Otherwise
                  s/n#S/ #S/ # if the second line starts with #S, replace the newline with space.
                  t start # If the above replacement occurred, go to start (to add another line).
                  # Otherwise
                  # (i.e when non-empty line not starting with #S occurred)
                  P # print the pattern space up to the first newline and...
                  D # delete the initial segment of the pattern space
                  # through the first newline (i.e. everything just printed),
                  # and start the next cycle with the resultant pattern space
                  # and without reading any new input
                  # (in our case the new input will be explicitly read by N then).
                  '





                  share|improve this answer



























                    up vote
                    4
                    down vote













                    This is a portable solution with POSIX sed, implementing the following rules:




                    • empty lines shall be deleted;

                    • any line starting with #S shall be merged with the previous non-empty line, with a single space character between them, unless there is no previous non-empty line.


                    The code:



                    <data sed '/^$/ d; :start; N; s/n$//; t start; s/n#S/ #S/; t start; P; D'


                    The same with comments (still working code):



                    <data sed '
                    /^$/ d # If empty line read, delete it and start a new cycle.
                    :start # A label.
                    N # Read additional line, there are now two lines in the pattern space.
                    s/n$// # If the second line is empty, replace the newline with nothing.
                    t start # If the above replacement occurred, go to start (to add another line).
                    # Otherwise
                    s/n#S/ #S/ # if the second line starts with #S, replace the newline with space.
                    t start # If the above replacement occurred, go to start (to add another line).
                    # Otherwise
                    # (i.e when non-empty line not starting with #S occurred)
                    P # print the pattern space up to the first newline and...
                    D # delete the initial segment of the pattern space
                    # through the first newline (i.e. everything just printed),
                    # and start the next cycle with the resultant pattern space
                    # and without reading any new input
                    # (in our case the new input will be explicitly read by N then).
                    '





                    share|improve this answer

























                      up vote
                      4
                      down vote










                      up vote
                      4
                      down vote









                      This is a portable solution with POSIX sed, implementing the following rules:




                      • empty lines shall be deleted;

                      • any line starting with #S shall be merged with the previous non-empty line, with a single space character between them, unless there is no previous non-empty line.


                      The code:



                      <data sed '/^$/ d; :start; N; s/n$//; t start; s/n#S/ #S/; t start; P; D'


                      The same with comments (still working code):



                      <data sed '
                      /^$/ d # If empty line read, delete it and start a new cycle.
                      :start # A label.
                      N # Read additional line, there are now two lines in the pattern space.
                      s/n$// # If the second line is empty, replace the newline with nothing.
                      t start # If the above replacement occurred, go to start (to add another line).
                      # Otherwise
                      s/n#S/ #S/ # if the second line starts with #S, replace the newline with space.
                      t start # If the above replacement occurred, go to start (to add another line).
                      # Otherwise
                      # (i.e when non-empty line not starting with #S occurred)
                      P # print the pattern space up to the first newline and...
                      D # delete the initial segment of the pattern space
                      # through the first newline (i.e. everything just printed),
                      # and start the next cycle with the resultant pattern space
                      # and without reading any new input
                      # (in our case the new input will be explicitly read by N then).
                      '





                      share|improve this answer














                      This is a portable solution with POSIX sed, implementing the following rules:




                      • empty lines shall be deleted;

                      • any line starting with #S shall be merged with the previous non-empty line, with a single space character between them, unless there is no previous non-empty line.


                      The code:



                      <data sed '/^$/ d; :start; N; s/n$//; t start; s/n#S/ #S/; t start; P; D'


                      The same with comments (still working code):



                      <data sed '
                      /^$/ d # If empty line read, delete it and start a new cycle.
                      :start # A label.
                      N # Read additional line, there are now two lines in the pattern space.
                      s/n$// # If the second line is empty, replace the newline with nothing.
                      t start # If the above replacement occurred, go to start (to add another line).
                      # Otherwise
                      s/n#S/ #S/ # if the second line starts with #S, replace the newline with space.
                      t start # If the above replacement occurred, go to start (to add another line).
                      # Otherwise
                      # (i.e when non-empty line not starting with #S occurred)
                      P # print the pattern space up to the first newline and...
                      D # delete the initial segment of the pattern space
                      # through the first newline (i.e. everything just printed),
                      # and start the next cycle with the resultant pattern space
                      # and without reading any new input
                      # (in our case the new input will be explicitly read by N then).
                      '






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 11 hours ago

























                      answered 11 hours ago









                      Kamil Maciorowski

                      22.6k155072




                      22.6k155072






















                          up vote
                          3
                          down vote













                          With paste (this requires to always have groups of 4 lines):



                           paste -s -d '   n' data


                          In slo-mo:





                          • paste -s concatenates the lines from the file


                          • -d specifies characters to be inserted as delimiters. When there are several characters, they are used in a round-robin fashion, so with 3 spaces and a LF:


                            • the first space is used on the first splice (N to #S),

                            • the second space is used on the second splice (#S to #S),

                            • the third space is used on the thrid splice (#S to blank line),

                            • the last delimiter, a LF, is used on the fourth splice (blank line to N)

                            • and the cycle repeats for the next 4 lines.








                          share|improve this answer

























                            up vote
                            3
                            down vote













                            With paste (this requires to always have groups of 4 lines):



                             paste -s -d '   n' data


                            In slo-mo:





                            • paste -s concatenates the lines from the file


                            • -d specifies characters to be inserted as delimiters. When there are several characters, they are used in a round-robin fashion, so with 3 spaces and a LF:


                              • the first space is used on the first splice (N to #S),

                              • the second space is used on the second splice (#S to #S),

                              • the third space is used on the thrid splice (#S to blank line),

                              • the last delimiter, a LF, is used on the fourth splice (blank line to N)

                              • and the cycle repeats for the next 4 lines.








                            share|improve this answer























                              up vote
                              3
                              down vote










                              up vote
                              3
                              down vote









                              With paste (this requires to always have groups of 4 lines):



                               paste -s -d '   n' data


                              In slo-mo:





                              • paste -s concatenates the lines from the file


                              • -d specifies characters to be inserted as delimiters. When there are several characters, they are used in a round-robin fashion, so with 3 spaces and a LF:


                                • the first space is used on the first splice (N to #S),

                                • the second space is used on the second splice (#S to #S),

                                • the third space is used on the thrid splice (#S to blank line),

                                • the last delimiter, a LF, is used on the fourth splice (blank line to N)

                                • and the cycle repeats for the next 4 lines.








                              share|improve this answer












                              With paste (this requires to always have groups of 4 lines):



                               paste -s -d '   n' data


                              In slo-mo:





                              • paste -s concatenates the lines from the file


                              • -d specifies characters to be inserted as delimiters. When there are several characters, they are used in a round-robin fashion, so with 3 spaces and a LF:


                                • the first space is used on the first splice (N to #S),

                                • the second space is used on the second splice (#S to #S),

                                • the third space is used on the thrid splice (#S to blank line),

                                • the last delimiter, a LF, is used on the fourth splice (blank line to N)

                                • and the cycle repeats for the next 4 lines.









                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 15 hours ago









                              xenoid

                              3,4973618




                              3,4973618






















                                  up vote
                                  3
                                  down vote













                                  Using Perl:



                                  perl -0 -ape 's/R(?=RN|#)/ /g' file.txt
                                  N 12344;PE 9.9999999;... #S 0 0 31 44 75 130 165 196... #S_+ "2 5 2 3 3 1 1 2 3 1 2 2...
                                  N 12345;PE 9.9999999;... #S 0 0 34 57 84 133 152... #S_+ "1 0 1 1 2 3 0 0 0...
                                  N 12346;PE 9.9999999;... #S 0 0 31 44 73 140 169... #S_+ "3 3 4 0 0 2 1 2 4...
                                  N 25104;PE 9.9999999;... #S 0 0 36 52 102 108 145... #S_+ "1 1 0 1 0 0 3 0 1...
                                  N 25105;PE 9.9999999;... #S 0 0 32 58 88 130 143...


                                  Regex explain:



                                  s/              : substitute
                                  R : any kind of line break (ie. r, n, rn)
                                  (?= : positive lookahead, zero-length assertion that make sure we have after
                                  RN : a line break followed by letter N
                                  | : OR
                                  # : # character
                                  ) : end lookahead
                                  / /g : replace with a space, global





                                  share|improve this answer























                                  • @KamilMaciorowski: Yes, you're right. Fixed
                                    – Toto
                                    13 hours ago















                                  up vote
                                  3
                                  down vote













                                  Using Perl:



                                  perl -0 -ape 's/R(?=RN|#)/ /g' file.txt
                                  N 12344;PE 9.9999999;... #S 0 0 31 44 75 130 165 196... #S_+ "2 5 2 3 3 1 1 2 3 1 2 2...
                                  N 12345;PE 9.9999999;... #S 0 0 34 57 84 133 152... #S_+ "1 0 1 1 2 3 0 0 0...
                                  N 12346;PE 9.9999999;... #S 0 0 31 44 73 140 169... #S_+ "3 3 4 0 0 2 1 2 4...
                                  N 25104;PE 9.9999999;... #S 0 0 36 52 102 108 145... #S_+ "1 1 0 1 0 0 3 0 1...
                                  N 25105;PE 9.9999999;... #S 0 0 32 58 88 130 143...


                                  Regex explain:



                                  s/              : substitute
                                  R : any kind of line break (ie. r, n, rn)
                                  (?= : positive lookahead, zero-length assertion that make sure we have after
                                  RN : a line break followed by letter N
                                  | : OR
                                  # : # character
                                  ) : end lookahead
                                  / /g : replace with a space, global





                                  share|improve this answer























                                  • @KamilMaciorowski: Yes, you're right. Fixed
                                    – Toto
                                    13 hours ago













                                  up vote
                                  3
                                  down vote










                                  up vote
                                  3
                                  down vote









                                  Using Perl:



                                  perl -0 -ape 's/R(?=RN|#)/ /g' file.txt
                                  N 12344;PE 9.9999999;... #S 0 0 31 44 75 130 165 196... #S_+ "2 5 2 3 3 1 1 2 3 1 2 2...
                                  N 12345;PE 9.9999999;... #S 0 0 34 57 84 133 152... #S_+ "1 0 1 1 2 3 0 0 0...
                                  N 12346;PE 9.9999999;... #S 0 0 31 44 73 140 169... #S_+ "3 3 4 0 0 2 1 2 4...
                                  N 25104;PE 9.9999999;... #S 0 0 36 52 102 108 145... #S_+ "1 1 0 1 0 0 3 0 1...
                                  N 25105;PE 9.9999999;... #S 0 0 32 58 88 130 143...


                                  Regex explain:



                                  s/              : substitute
                                  R : any kind of line break (ie. r, n, rn)
                                  (?= : positive lookahead, zero-length assertion that make sure we have after
                                  RN : a line break followed by letter N
                                  | : OR
                                  # : # character
                                  ) : end lookahead
                                  / /g : replace with a space, global





                                  share|improve this answer














                                  Using Perl:



                                  perl -0 -ape 's/R(?=RN|#)/ /g' file.txt
                                  N 12344;PE 9.9999999;... #S 0 0 31 44 75 130 165 196... #S_+ "2 5 2 3 3 1 1 2 3 1 2 2...
                                  N 12345;PE 9.9999999;... #S 0 0 34 57 84 133 152... #S_+ "1 0 1 1 2 3 0 0 0...
                                  N 12346;PE 9.9999999;... #S 0 0 31 44 73 140 169... #S_+ "3 3 4 0 0 2 1 2 4...
                                  N 25104;PE 9.9999999;... #S 0 0 36 52 102 108 145... #S_+ "1 1 0 1 0 0 3 0 1...
                                  N 25105;PE 9.9999999;... #S 0 0 32 58 88 130 143...


                                  Regex explain:



                                  s/              : substitute
                                  R : any kind of line break (ie. r, n, rn)
                                  (?= : positive lookahead, zero-length assertion that make sure we have after
                                  RN : a line break followed by letter N
                                  | : OR
                                  # : # character
                                  ) : end lookahead
                                  / /g : replace with a space, global






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited 13 hours ago

























                                  answered 14 hours ago









                                  Toto

                                  3,28291125




                                  3,28291125












                                  • @KamilMaciorowski: Yes, you're right. Fixed
                                    – Toto
                                    13 hours ago


















                                  • @KamilMaciorowski: Yes, you're right. Fixed
                                    – Toto
                                    13 hours ago
















                                  @KamilMaciorowski: Yes, you're right. Fixed
                                  – Toto
                                  13 hours ago




                                  @KamilMaciorowski: Yes, you're right. Fixed
                                  – Toto
                                  13 hours ago










                                  up vote
                                  1
                                  down vote













                                  awk (gawk)



                                  As usually other than sed you can use awk (and in many different ways...)



                                  awk 'ORS=" "; NR % 4 == 0 && ORS="n" ' data


                                  where





                                  • ORS=" " fixes the output record separator, by default a newline, to a space (you can change)


                                  • NR % 4 == 0 && ORS="n" each 4th line it fixes back to the newline n

                                  • If nothing else is specified awk prints the full line


                                  • data is your data file.


                                  If you want you can use regex as in sed (in a similar way).






                                  share|improve this answer

























                                    up vote
                                    1
                                    down vote













                                    awk (gawk)



                                    As usually other than sed you can use awk (and in many different ways...)



                                    awk 'ORS=" "; NR % 4 == 0 && ORS="n" ' data


                                    where





                                    • ORS=" " fixes the output record separator, by default a newline, to a space (you can change)


                                    • NR % 4 == 0 && ORS="n" each 4th line it fixes back to the newline n

                                    • If nothing else is specified awk prints the full line


                                    • data is your data file.


                                    If you want you can use regex as in sed (in a similar way).






                                    share|improve this answer























                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote









                                      awk (gawk)



                                      As usually other than sed you can use awk (and in many different ways...)



                                      awk 'ORS=" "; NR % 4 == 0 && ORS="n" ' data


                                      where





                                      • ORS=" " fixes the output record separator, by default a newline, to a space (you can change)


                                      • NR % 4 == 0 && ORS="n" each 4th line it fixes back to the newline n

                                      • If nothing else is specified awk prints the full line


                                      • data is your data file.


                                      If you want you can use regex as in sed (in a similar way).






                                      share|improve this answer












                                      awk (gawk)



                                      As usually other than sed you can use awk (and in many different ways...)



                                      awk 'ORS=" "; NR % 4 == 0 && ORS="n" ' data


                                      where





                                      • ORS=" " fixes the output record separator, by default a newline, to a space (you can change)


                                      • NR % 4 == 0 && ORS="n" each 4th line it fixes back to the newline n

                                      • If nothing else is specified awk prints the full line


                                      • data is your data file.


                                      If you want you can use regex as in sed (in a similar way).







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 7 hours ago









                                      Hastur

                                      12.9k53266




                                      12.9k53266






















                                          up vote
                                          0
                                          down vote













                                          You can do it with any text editor that support regular expressions like Notepad++.



                                          The new line is just simple non-printable character or two characters. In Windows usually CarrigeReturn and LineFeed and in Unix based system usually LineFeed only.



                                          To see them you need to turn on showing non-printable character (usually a Paragraph icon)
                                          See here: https://imgur.com/cqiTvrp



                                          Now what you need to do is to use regular expression replacer (CTRL + H) to replace CRLF#S to #S.
                                          The symbol for CR is r and for LF is n. So you gonna end up with rn#S or n#S to #S.
                                          https://imgur.com/GoeVn70



                                          Or you can replace it to SPACE if you need.






                                          share|improve this answer








                                          New contributor




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


















                                          • The question is tagged "Linux"....
                                            – xenoid
                                            15 hours ago










                                          • I think regular expressions in Geany are the same. Is used Notepad++ as an example beacuse I am currently at Windows.
                                            – KaRolthas
                                            15 hours ago












                                          • The question also asks for a command-line utility...
                                            – xenoid
                                            15 hours ago










                                          • Nice, works. I need to somehow process at least few files now so even Notepad++ helps when I am working on my other machine with Windows. thanks
                                            – Juhele
                                            15 hours ago















                                          up vote
                                          0
                                          down vote













                                          You can do it with any text editor that support regular expressions like Notepad++.



                                          The new line is just simple non-printable character or two characters. In Windows usually CarrigeReturn and LineFeed and in Unix based system usually LineFeed only.



                                          To see them you need to turn on showing non-printable character (usually a Paragraph icon)
                                          See here: https://imgur.com/cqiTvrp



                                          Now what you need to do is to use regular expression replacer (CTRL + H) to replace CRLF#S to #S.
                                          The symbol for CR is r and for LF is n. So you gonna end up with rn#S or n#S to #S.
                                          https://imgur.com/GoeVn70



                                          Or you can replace it to SPACE if you need.






                                          share|improve this answer








                                          New contributor




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


















                                          • The question is tagged "Linux"....
                                            – xenoid
                                            15 hours ago










                                          • I think regular expressions in Geany are the same. Is used Notepad++ as an example beacuse I am currently at Windows.
                                            – KaRolthas
                                            15 hours ago












                                          • The question also asks for a command-line utility...
                                            – xenoid
                                            15 hours ago










                                          • Nice, works. I need to somehow process at least few files now so even Notepad++ helps when I am working on my other machine with Windows. thanks
                                            – Juhele
                                            15 hours ago













                                          up vote
                                          0
                                          down vote










                                          up vote
                                          0
                                          down vote









                                          You can do it with any text editor that support regular expressions like Notepad++.



                                          The new line is just simple non-printable character or two characters. In Windows usually CarrigeReturn and LineFeed and in Unix based system usually LineFeed only.



                                          To see them you need to turn on showing non-printable character (usually a Paragraph icon)
                                          See here: https://imgur.com/cqiTvrp



                                          Now what you need to do is to use regular expression replacer (CTRL + H) to replace CRLF#S to #S.
                                          The symbol for CR is r and for LF is n. So you gonna end up with rn#S or n#S to #S.
                                          https://imgur.com/GoeVn70



                                          Or you can replace it to SPACE if you need.






                                          share|improve this answer








                                          New contributor




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









                                          You can do it with any text editor that support regular expressions like Notepad++.



                                          The new line is just simple non-printable character or two characters. In Windows usually CarrigeReturn and LineFeed and in Unix based system usually LineFeed only.



                                          To see them you need to turn on showing non-printable character (usually a Paragraph icon)
                                          See here: https://imgur.com/cqiTvrp



                                          Now what you need to do is to use regular expression replacer (CTRL + H) to replace CRLF#S to #S.
                                          The symbol for CR is r and for LF is n. So you gonna end up with rn#S or n#S to #S.
                                          https://imgur.com/GoeVn70



                                          Or you can replace it to SPACE if you need.







                                          share|improve this answer








                                          New contributor




                                          KaRolthas 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 answer



                                          share|improve this answer






                                          New contributor




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









                                          answered 15 hours ago









                                          KaRolthas

                                          1




                                          1




                                          New contributor




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





                                          New contributor





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






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












                                          • The question is tagged "Linux"....
                                            – xenoid
                                            15 hours ago










                                          • I think regular expressions in Geany are the same. Is used Notepad++ as an example beacuse I am currently at Windows.
                                            – KaRolthas
                                            15 hours ago












                                          • The question also asks for a command-line utility...
                                            – xenoid
                                            15 hours ago










                                          • Nice, works. I need to somehow process at least few files now so even Notepad++ helps when I am working on my other machine with Windows. thanks
                                            – Juhele
                                            15 hours ago


















                                          • The question is tagged "Linux"....
                                            – xenoid
                                            15 hours ago










                                          • I think regular expressions in Geany are the same. Is used Notepad++ as an example beacuse I am currently at Windows.
                                            – KaRolthas
                                            15 hours ago












                                          • The question also asks for a command-line utility...
                                            – xenoid
                                            15 hours ago










                                          • Nice, works. I need to somehow process at least few files now so even Notepad++ helps when I am working on my other machine with Windows. thanks
                                            – Juhele
                                            15 hours ago
















                                          The question is tagged "Linux"....
                                          – xenoid
                                          15 hours ago




                                          The question is tagged "Linux"....
                                          – xenoid
                                          15 hours ago












                                          I think regular expressions in Geany are the same. Is used Notepad++ as an example beacuse I am currently at Windows.
                                          – KaRolthas
                                          15 hours ago






                                          I think regular expressions in Geany are the same. Is used Notepad++ as an example beacuse I am currently at Windows.
                                          – KaRolthas
                                          15 hours ago














                                          The question also asks for a command-line utility...
                                          – xenoid
                                          15 hours ago




                                          The question also asks for a command-line utility...
                                          – xenoid
                                          15 hours ago












                                          Nice, works. I need to somehow process at least few files now so even Notepad++ helps when I am working on my other machine with Windows. thanks
                                          – Juhele
                                          15 hours ago




                                          Nice, works. I need to somehow process at least few files now so even Notepad++ helps when I am working on my other machine with Windows. thanks
                                          – Juhele
                                          15 hours ago


















                                           

                                          draft saved


                                          draft discarded



















































                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1377291%2fhow-can-i-replace-end-line-with-fixed-text-when-the-next-line-begins-with-a-defi%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

                                          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                          Calculate evaluation metrics using cross_val_predict sklearn

                                          Insert data from modal to MySQL (multiple modal on website)