sed: joining lines depending on the second one

This might work for you:

sed 'N;s/\n\s*+//;P;D' file

These are actually four commands:

  • N
    Append line from the input file to the pattern space
  • s/\n\s*+//
    Remove newline, following whitespace and the plus
  • P
    print line from the pattern space until the first newline
  • D
    delete line from the pattern space until the first newline, e.g. the part which was just printed

The relevant manual page parts are

  • Selecting lines by numbers
  • Addresses overview
  • Multiline techniques - using D,G,H,N,P to process multiple lines

Doing this in sed is certainly a good exercise, but it's pretty trivial in perl:

perl -0777 -pe 's/\n\s*\+//g' input

Tags:

Sed