Sed to merge lines that are delimited with a token [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite













This question already has an answer here:



  • Replace comma with newline following with just a command and space

    8 answers



I'm trying to write a script that uses sed to process lines in a text file to generate sample documentation. I've got most of the script working but I'm stuck with one edge case. Consider the following file



line-1
line-2, part2
line-3-should-be-a-very-long,
line-3-continued
line-4


The problem is that some, but not all, lines end in a special token (it happens to be a comma). The token indicates that the line should be concatenated with following one to produce one long line.



So in my example line-3-should-be-a-very-long, should be concatenated with line-3-continued to give me line-3-should-be-a-very-long, line-3-continued (I do want to keep the comma). There is no special action on line 2 even though it contains a comma which is NOT at the end of the line.



The rest of the processing is done by piping a few sed and grep commands together, so a sed solution would fit well.







share|improve this question













marked as duplicate by αғsнιη, Philippos, roaima, G-Man, Rui F Ribeiro Apr 21 at 17:46


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    @αғsнιη Not a duplicate if the lines may contain other commas (not just at the end).
    – Kusalananda
    Apr 20 at 14:50











  • @Kusalananda is correct
    – Stormcloud
    Apr 20 at 14:57










  • @Kusalananda I didn't get your point. if a line contain other comma other than at the end then that line not required to join and as OP sample s/he wants to join every lines if previous line ends with comma. Am I missing something here?
    – Î±Ò“sнιη
    Apr 20 at 15:00











  • @αғsнιη Sorry, I might have been a bit too quick on the trigger there. I made the comment because the top answer (and some others) of the proposed duplicate assumes that commas do not occur in the filename. Here, we have a generic "line", which means that there may be commas on the line in other positions than at the end. The top answer of the other question would not work in that case. I'm not going to push this point harder than this, so if more people feels that it is indeed a dupe, well, then it is a dupe.
    – Kusalananda
    Apr 20 at 15:16















up vote
2
down vote

favorite













This question already has an answer here:



  • Replace comma with newline following with just a command and space

    8 answers



I'm trying to write a script that uses sed to process lines in a text file to generate sample documentation. I've got most of the script working but I'm stuck with one edge case. Consider the following file



line-1
line-2, part2
line-3-should-be-a-very-long,
line-3-continued
line-4


The problem is that some, but not all, lines end in a special token (it happens to be a comma). The token indicates that the line should be concatenated with following one to produce one long line.



So in my example line-3-should-be-a-very-long, should be concatenated with line-3-continued to give me line-3-should-be-a-very-long, line-3-continued (I do want to keep the comma). There is no special action on line 2 even though it contains a comma which is NOT at the end of the line.



The rest of the processing is done by piping a few sed and grep commands together, so a sed solution would fit well.







share|improve this question













marked as duplicate by αғsнιη, Philippos, roaima, G-Man, Rui F Ribeiro Apr 21 at 17:46


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    @αғsнιη Not a duplicate if the lines may contain other commas (not just at the end).
    – Kusalananda
    Apr 20 at 14:50











  • @Kusalananda is correct
    – Stormcloud
    Apr 20 at 14:57










  • @Kusalananda I didn't get your point. if a line contain other comma other than at the end then that line not required to join and as OP sample s/he wants to join every lines if previous line ends with comma. Am I missing something here?
    – Î±Ò“sнιη
    Apr 20 at 15:00











  • @αғsнιη Sorry, I might have been a bit too quick on the trigger there. I made the comment because the top answer (and some others) of the proposed duplicate assumes that commas do not occur in the filename. Here, we have a generic "line", which means that there may be commas on the line in other positions than at the end. The top answer of the other question would not work in that case. I'm not going to push this point harder than this, so if more people feels that it is indeed a dupe, well, then it is a dupe.
    – Kusalananda
    Apr 20 at 15:16













up vote
2
down vote

favorite









up vote
2
down vote

favorite












This question already has an answer here:



  • Replace comma with newline following with just a command and space

    8 answers



I'm trying to write a script that uses sed to process lines in a text file to generate sample documentation. I've got most of the script working but I'm stuck with one edge case. Consider the following file



line-1
line-2, part2
line-3-should-be-a-very-long,
line-3-continued
line-4


The problem is that some, but not all, lines end in a special token (it happens to be a comma). The token indicates that the line should be concatenated with following one to produce one long line.



So in my example line-3-should-be-a-very-long, should be concatenated with line-3-continued to give me line-3-should-be-a-very-long, line-3-continued (I do want to keep the comma). There is no special action on line 2 even though it contains a comma which is NOT at the end of the line.



The rest of the processing is done by piping a few sed and grep commands together, so a sed solution would fit well.







share|improve this question














This question already has an answer here:



  • Replace comma with newline following with just a command and space

    8 answers



I'm trying to write a script that uses sed to process lines in a text file to generate sample documentation. I've got most of the script working but I'm stuck with one edge case. Consider the following file



line-1
line-2, part2
line-3-should-be-a-very-long,
line-3-continued
line-4


The problem is that some, but not all, lines end in a special token (it happens to be a comma). The token indicates that the line should be concatenated with following one to produce one long line.



So in my example line-3-should-be-a-very-long, should be concatenated with line-3-continued to give me line-3-should-be-a-very-long, line-3-continued (I do want to keep the comma). There is no special action on line 2 even though it contains a comma which is NOT at the end of the line.



The rest of the processing is done by piping a few sed and grep commands together, so a sed solution would fit well.





This question already has an answer here:



  • Replace comma with newline following with just a command and space

    8 answers









share|improve this question












share|improve this question




share|improve this question








edited Apr 20 at 15:01
























asked Apr 20 at 13:18









Stormcloud

1134




1134




marked as duplicate by αғsнιη, Philippos, roaima, G-Man, Rui F Ribeiro Apr 21 at 17:46


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by αғsнιη, Philippos, roaima, G-Man, Rui F Ribeiro Apr 21 at 17:46


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1




    @αғsнιη Not a duplicate if the lines may contain other commas (not just at the end).
    – Kusalananda
    Apr 20 at 14:50











  • @Kusalananda is correct
    – Stormcloud
    Apr 20 at 14:57










  • @Kusalananda I didn't get your point. if a line contain other comma other than at the end then that line not required to join and as OP sample s/he wants to join every lines if previous line ends with comma. Am I missing something here?
    – Î±Ò“sнιη
    Apr 20 at 15:00











  • @αғsнιη Sorry, I might have been a bit too quick on the trigger there. I made the comment because the top answer (and some others) of the proposed duplicate assumes that commas do not occur in the filename. Here, we have a generic "line", which means that there may be commas on the line in other positions than at the end. The top answer of the other question would not work in that case. I'm not going to push this point harder than this, so if more people feels that it is indeed a dupe, well, then it is a dupe.
    – Kusalananda
    Apr 20 at 15:16













  • 1




    @αғsнιη Not a duplicate if the lines may contain other commas (not just at the end).
    – Kusalananda
    Apr 20 at 14:50











  • @Kusalananda is correct
    – Stormcloud
    Apr 20 at 14:57










  • @Kusalananda I didn't get your point. if a line contain other comma other than at the end then that line not required to join and as OP sample s/he wants to join every lines if previous line ends with comma. Am I missing something here?
    – Î±Ò“sнιη
    Apr 20 at 15:00











  • @αғsнιη Sorry, I might have been a bit too quick on the trigger there. I made the comment because the top answer (and some others) of the proposed duplicate assumes that commas do not occur in the filename. Here, we have a generic "line", which means that there may be commas on the line in other positions than at the end. The top answer of the other question would not work in that case. I'm not going to push this point harder than this, so if more people feels that it is indeed a dupe, well, then it is a dupe.
    – Kusalananda
    Apr 20 at 15:16








1




1




@αғsнιη Not a duplicate if the lines may contain other commas (not just at the end).
– Kusalananda
Apr 20 at 14:50





@αғsнιη Not a duplicate if the lines may contain other commas (not just at the end).
– Kusalananda
Apr 20 at 14:50













@Kusalananda is correct
– Stormcloud
Apr 20 at 14:57




@Kusalananda is correct
– Stormcloud
Apr 20 at 14:57












@Kusalananda I didn't get your point. if a line contain other comma other than at the end then that line not required to join and as OP sample s/he wants to join every lines if previous line ends with comma. Am I missing something here?
– Î±Ò“sнιη
Apr 20 at 15:00





@Kusalananda I didn't get your point. if a line contain other comma other than at the end then that line not required to join and as OP sample s/he wants to join every lines if previous line ends with comma. Am I missing something here?
– Î±Ò“sнιη
Apr 20 at 15:00













@αғsнιη Sorry, I might have been a bit too quick on the trigger there. I made the comment because the top answer (and some others) of the proposed duplicate assumes that commas do not occur in the filename. Here, we have a generic "line", which means that there may be commas on the line in other positions than at the end. The top answer of the other question would not work in that case. I'm not going to push this point harder than this, so if more people feels that it is indeed a dupe, well, then it is a dupe.
– Kusalananda
Apr 20 at 15:16





@αғsнιη Sorry, I might have been a bit too quick on the trigger there. I made the comment because the top answer (and some others) of the proposed duplicate assumes that commas do not occur in the filename. Here, we have a generic "line", which means that there may be commas on the line in other positions than at the end. The top answer of the other question would not work in that case. I'm not going to push this point harder than this, so if more people feels that it is indeed a dupe, well, then it is a dupe.
– Kusalananda
Apr 20 at 15:16











2 Answers
2






active

oldest

votes

















up vote
5
down vote



accepted










$ sed '/,$/N;s/n//;' file
line-1
line-2
line-3-should-be-a-very-long, line-3-continued
line-4


If the blanks should be deleted:



$ sed '/,$/N;s/n[[:blank:]]*//;' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued
line-4


(if you want a single space to remain between the lines, replace // in the code by / /)



If lines can be continued multiple times, as in



line-1
line-2
line-3-should-be-a-very-long,
line-3-continued,
line-3-continued-further
line-4


then,



$ sed '/,$/:loop;N;s/n[[:blank:]]*//;/,$/bloop;' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued,line-3-continued-further
line-4


This last sed script explained with annotations:



/,$/ # if the current line ends with a comma, then...
:loop # define label "loop"
N # append next line from input (a newline will be inserted in-between)
s/n[[:blank:]]*// # delete that newline and any blanks (tabs or spaces) directly after it
/,$/bloop # if the line now ends with comma, branch to the "loop" label

# implicit output of (possibly) modified line at end





share|improve this answer






























    up vote
    0
    down vote













    sed '/,$/N;s/n[[:blank:]]+/ /' file


    When you see a line ending with a comma, read in the next line then replace the newline and leading whitespace of the next line with a single space.






    share|improve this answer





















    • The OP mentioned that comma is just a special token that implies concatenation. The comma should be replaced as well
      – Bruno9779
      Apr 20 at 13:59










    • @Bruno9779 The OP says "I do want to keep the comma".
      – Kusalananda
      Apr 20 at 14:08

















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote



    accepted










    $ sed '/,$/N;s/n//;' file
    line-1
    line-2
    line-3-should-be-a-very-long, line-3-continued
    line-4


    If the blanks should be deleted:



    $ sed '/,$/N;s/n[[:blank:]]*//;' file
    line-1
    line-2
    line-3-should-be-a-very-long,line-3-continued
    line-4


    (if you want a single space to remain between the lines, replace // in the code by / /)



    If lines can be continued multiple times, as in



    line-1
    line-2
    line-3-should-be-a-very-long,
    line-3-continued,
    line-3-continued-further
    line-4


    then,



    $ sed '/,$/:loop;N;s/n[[:blank:]]*//;/,$/bloop;' file
    line-1
    line-2
    line-3-should-be-a-very-long,line-3-continued,line-3-continued-further
    line-4


    This last sed script explained with annotations:



    /,$/ # if the current line ends with a comma, then...
    :loop # define label "loop"
    N # append next line from input (a newline will be inserted in-between)
    s/n[[:blank:]]*// # delete that newline and any blanks (tabs or spaces) directly after it
    /,$/bloop # if the line now ends with comma, branch to the "loop" label

    # implicit output of (possibly) modified line at end





    share|improve this answer



























      up vote
      5
      down vote



      accepted










      $ sed '/,$/N;s/n//;' file
      line-1
      line-2
      line-3-should-be-a-very-long, line-3-continued
      line-4


      If the blanks should be deleted:



      $ sed '/,$/N;s/n[[:blank:]]*//;' file
      line-1
      line-2
      line-3-should-be-a-very-long,line-3-continued
      line-4


      (if you want a single space to remain between the lines, replace // in the code by / /)



      If lines can be continued multiple times, as in



      line-1
      line-2
      line-3-should-be-a-very-long,
      line-3-continued,
      line-3-continued-further
      line-4


      then,



      $ sed '/,$/:loop;N;s/n[[:blank:]]*//;/,$/bloop;' file
      line-1
      line-2
      line-3-should-be-a-very-long,line-3-continued,line-3-continued-further
      line-4


      This last sed script explained with annotations:



      /,$/ # if the current line ends with a comma, then...
      :loop # define label "loop"
      N # append next line from input (a newline will be inserted in-between)
      s/n[[:blank:]]*// # delete that newline and any blanks (tabs or spaces) directly after it
      /,$/bloop # if the line now ends with comma, branch to the "loop" label

      # implicit output of (possibly) modified line at end





      share|improve this answer

























        up vote
        5
        down vote



        accepted







        up vote
        5
        down vote



        accepted






        $ sed '/,$/N;s/n//;' file
        line-1
        line-2
        line-3-should-be-a-very-long, line-3-continued
        line-4


        If the blanks should be deleted:



        $ sed '/,$/N;s/n[[:blank:]]*//;' file
        line-1
        line-2
        line-3-should-be-a-very-long,line-3-continued
        line-4


        (if you want a single space to remain between the lines, replace // in the code by / /)



        If lines can be continued multiple times, as in



        line-1
        line-2
        line-3-should-be-a-very-long,
        line-3-continued,
        line-3-continued-further
        line-4


        then,



        $ sed '/,$/:loop;N;s/n[[:blank:]]*//;/,$/bloop;' file
        line-1
        line-2
        line-3-should-be-a-very-long,line-3-continued,line-3-continued-further
        line-4


        This last sed script explained with annotations:



        /,$/ # if the current line ends with a comma, then...
        :loop # define label "loop"
        N # append next line from input (a newline will be inserted in-between)
        s/n[[:blank:]]*// # delete that newline and any blanks (tabs or spaces) directly after it
        /,$/bloop # if the line now ends with comma, branch to the "loop" label

        # implicit output of (possibly) modified line at end





        share|improve this answer















        $ sed '/,$/N;s/n//;' file
        line-1
        line-2
        line-3-should-be-a-very-long, line-3-continued
        line-4


        If the blanks should be deleted:



        $ sed '/,$/N;s/n[[:blank:]]*//;' file
        line-1
        line-2
        line-3-should-be-a-very-long,line-3-continued
        line-4


        (if you want a single space to remain between the lines, replace // in the code by / /)



        If lines can be continued multiple times, as in



        line-1
        line-2
        line-3-should-be-a-very-long,
        line-3-continued,
        line-3-continued-further
        line-4


        then,



        $ sed '/,$/:loop;N;s/n[[:blank:]]*//;/,$/bloop;' file
        line-1
        line-2
        line-3-should-be-a-very-long,line-3-continued,line-3-continued-further
        line-4


        This last sed script explained with annotations:



        /,$/ # if the current line ends with a comma, then...
        :loop # define label "loop"
        N # append next line from input (a newline will be inserted in-between)
        s/n[[:blank:]]*// # delete that newline and any blanks (tabs or spaces) directly after it
        /,$/bloop # if the line now ends with comma, branch to the "loop" label

        # implicit output of (possibly) modified line at end






        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Apr 20 at 14:35


























        answered Apr 20 at 14:04









        Kusalananda

        102k13199315




        102k13199315






















            up vote
            0
            down vote













            sed '/,$/N;s/n[[:blank:]]+/ /' file


            When you see a line ending with a comma, read in the next line then replace the newline and leading whitespace of the next line with a single space.






            share|improve this answer





















            • The OP mentioned that comma is just a special token that implies concatenation. The comma should be replaced as well
              – Bruno9779
              Apr 20 at 13:59










            • @Bruno9779 The OP says "I do want to keep the comma".
              – Kusalananda
              Apr 20 at 14:08














            up vote
            0
            down vote













            sed '/,$/N;s/n[[:blank:]]+/ /' file


            When you see a line ending with a comma, read in the next line then replace the newline and leading whitespace of the next line with a single space.






            share|improve this answer





















            • The OP mentioned that comma is just a special token that implies concatenation. The comma should be replaced as well
              – Bruno9779
              Apr 20 at 13:59










            • @Bruno9779 The OP says "I do want to keep the comma".
              – Kusalananda
              Apr 20 at 14:08












            up vote
            0
            down vote










            up vote
            0
            down vote









            sed '/,$/N;s/n[[:blank:]]+/ /' file


            When you see a line ending with a comma, read in the next line then replace the newline and leading whitespace of the next line with a single space.






            share|improve this answer













            sed '/,$/N;s/n[[:blank:]]+/ /' file


            When you see a line ending with a comma, read in the next line then replace the newline and leading whitespace of the next line with a single space.







            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Apr 20 at 13:37









            glenn jackman

            45.9k265100




            45.9k265100











            • The OP mentioned that comma is just a special token that implies concatenation. The comma should be replaced as well
              – Bruno9779
              Apr 20 at 13:59










            • @Bruno9779 The OP says "I do want to keep the comma".
              – Kusalananda
              Apr 20 at 14:08
















            • The OP mentioned that comma is just a special token that implies concatenation. The comma should be replaced as well
              – Bruno9779
              Apr 20 at 13:59










            • @Bruno9779 The OP says "I do want to keep the comma".
              – Kusalananda
              Apr 20 at 14:08















            The OP mentioned that comma is just a special token that implies concatenation. The comma should be replaced as well
            – Bruno9779
            Apr 20 at 13:59




            The OP mentioned that comma is just a special token that implies concatenation. The comma should be replaced as well
            – Bruno9779
            Apr 20 at 13:59












            @Bruno9779 The OP says "I do want to keep the comma".
            – Kusalananda
            Apr 20 at 14:08




            @Bruno9779 The OP says "I do want to keep the comma".
            – Kusalananda
            Apr 20 at 14:08


            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)