This question already has an answer here:

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-2, part2

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.

marked as duplicate by αғsнιη, Philippos, roaima, G-Man, Rui F Ribeiro Apr 21 '18 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 '18 at 14:50
  • @Kusalananda is correct – Stormcloud Apr 20 '18 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 '18 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 '18 at 15:16
$ sed '/,$/{N;s/\n//;}' file
line-3-should-be-a-very-long,    line-3-continued

If the blanks should be deleted:

$ sed '/,$/{N;s/\n[[:blank:]]*//;}' file

(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



$ sed '/,$/{:loop;N;s/\n[[:blank:]]*//;/,$/bloop;}' file

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
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.

  • The OP mentioned that comma is just a special token that implies concatenation. The comma should be replaced as well – Bruno9779 Apr 20 '18 at 13:59
  • @Bruno9779 The OP says "I do want to keep the comma". – Kusalananda Apr 20 '18 at 14:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.