Q:

convert multiply lines between pattern to a comma separated string

I need help in processing data from STDIN (data is taken from another file with 'tail -f' plus grepped to filter out garbage). There are several lines between patterns:
    <DN>  589</DN>
    <DD>03.12.2014</DD>
    <ST>   </ST>
    <STC>0</STC>
    <STT>0</STT>
    <PU>5</PU>
    <OT>01</OT>
    <DSN></DSN>
    <NRA>40807,40820,426,30231,40818,30230</NRA>
    <GR>300 000-00&#13;&#10</GR>

then next block with DN/GR starts
I need to convert lines between  and  to a single line, comma-separated:
<DN>  589</DN>,<DD>03.12.2014</DD>,<ST>   </ST>,<STC>0</STC>,<STT>0</STT>,<PU>5</PU>,<OT>01</OT>,<DSN></DSN>,<NRA>40807,40820,426,30231,40818,30230</NRA>,<GR>300 000-00&#13;&#10</GR>

I need a one-liner with awk or sed or perl to do it and put result to STDOUT.
I've tried to do it, but failed due to lack of experience. Also tried to google and didn't find a working solution.

A:

sed -nr '/<DN>/,/<GR>/{ H; /<GR>/{ g; s%\n%,%g; s%^,%%; p; s%.*%%; h }; }' <<'EOSEQ'
<DN>  589</DN>
<DD>03.12.2014</DD>
<STC>0</STC>
<GR>300 000-00&#13;&#10</GR>
<DN>900</DN>
<DD>20.11.2014</DD>
<OT>01</OT>
<NRA>40807,40820,426,30231,40818,30230</NRA>
<GR>300 000-00&#13;&#10</GR>
EOSEQ

SED one-liner, as you wish :)

