I edited your sed, hope this helps.
sed -r '/\[$/ {:a;N;s/\]/&/;Ta;s/\n +//g}' sed -r ' # sed will apply the commands between '{}' only to lines that matches the address '/\[$/'. /\[$/ { # Set a mark with label 'a'. :a # N command, it appends a '\n' to the pattern space, # reads the next line of the input (file,stdin) and appends it to the pattern space. N # Substitute ']' for itself. If the substitution isn't made (if there isn't a ']' on the # pattern space), the 'T' command jumps to the 'a' label. # Here is the loop to put some lines (or all lines of a file) in the same line. # While there isn't a ']' in the pattern space (which is the last line OP wants to put # on the same line), sed will append '\n<next line>' to the pattern space. s/\]/&/ Ta # When the substitution is made, sed leaves the loop and applies other commands. # Substitute all occurrences (g flag) of new line character (with any # spaces after) for nothing. s/\n +//g }'