A little bit idiomatic but working with gnu awk:
awk '{printf "%s",(NF==1?$0 FS:(c==0?"":RS) $0 RS)} \
{(NF==1?++c:c=0)} \
c==4{printf "\n";c=0} \
END{printf "\n"}' file
#Output
1
4 5 6 7 19
20 22
24 26 27
29 30 31 32
34 40 50 56
58 100
234 235 270 500
1234 1235 1236 1237
2300 2303 2304 2307
2309
Explanation:
awk variables:
NF=Number of Fields
FS=Field Separator = space by default
RS=Record Separator= new line by default.
c=counter
Line1: {printf "%s",(NF==1?$0 FS:(c==0?"":RS) $0 RS)}: nested ternary if operations
#Single ternary if operation:
condition?true action:false action
#Nested if operations:
condition1?true action 1:(condition2:true action2:false action2) #nested ternary if operations
-------------------------[ ^ false action1 ^ ]
This can be explained in pseudocode like:
if NF==1 then print $0 and print FS
else (if c==0 then print "" else print RS) and print $0 and print RS again
Line 2: {(NF==1?++c:c=0)} : Another ternary if operation that can be expressed as:
If NF==1 (line has one field)
then increase counter c by one
else reset counter c.
Line 3 : c==4{printf "\n";c=0} Classic awk syntax : condition{action}
If counter c==4 then print a new line and reset counter c
Line 4: END{printf "\n"}' file : This justs prints a new line at the end of the script.