Q:

Print between two matches with awk

I want to rearrange the input with the following line but I keep getting ENDD on the wrong line.
awk '/output/ {printf $0":";flag=1;next} /ENDD/{flag=0;} flag {printf  $0 "," ;}{if (flag==0)print}' file

Input:
exam1
exam4 1
output
list2
frame 3
list 5
ENDD
col 1
cal 2
del 3

Output:
exam1
exam4 1
output:list2,frame 3,list 5,ENDD
col 1
cal 2
del 3

Desired Output:
exam1
exam4 1
output:list2,frame 3,list 5
ENDD
col 1
cal 2
del 3

A:

you can try
awk ' /output/ { printf "%s:",$0; flag=1; next } 
      /ENDD/{ print ""; flag=0; } 
      flag>1 { printf ","; }
      flag { printf "%s",$0; flag++ }
      !flag' file

you get

exam1
exam4 1
output:list2,frame 3,list 5
ENDD
col 1
cal 2
del 3

edit: another solution
awk -v ini=output -v end=ENDD '
  $0 ~ ini, $0 ~ end{
    printf ($0~ini? "%s:" : ($0~end? "\n%s\n" : (prev ~ ini? "%s" : ",%s"))), $0; 
    prev=$0; 
    next;
  } 1' file

