concatenate lines based on first char of next line

I'd do:

awk -v ORS= '
  NR>1 && !/,/ {print "\n"}
  {print}
  END {if (NR) print "\n"}' < file

That is, only prints that newline character that delimits the previous line if the current one does not start with a ,.

In any case, I wouldn't use a while read loop.


This is a classic use-case for sed, as explained in Sed One-Liners Explained, Part I: File Spacing, Numbering and Text Conversion and Substitution, 40. Append a line to the previous if it starts with an equal sign "=". (with the obvious modification of , for =)

sed -e :a -e '$!N;s/\n,/,/;ta' -e 'P;D' file
line0
line1
line2,line3
line4
line5,line6
line7,line8
line9
line10
line11

All you need to do is slurp the file and remove any newlines before commas:

$ perl -0777pe 's/\n,/,/g' file
line0
line1
line2,line3
line4
line5,line6
line7,line8
line9
line10
line11