How to transpose pieces of data in a column to multiple rows?

Hello Everyone,
I am very new to the world of regular expressions. I am trying to use grep/sed for the following:

Input file is something like this and there are multiple such files:

abc
1
2
3
4
5
***END***
abc
6
7
8
9
***END***
abc
10
11
12
***END***

I need to be able to edit the text in all of these files so that all the files look like this:

abc
1,2,3,4,5
***END***
abc
6,7,8,9
***END***
abc
10,11,12
***END***

Kindly help me. Your help is much appreciated!

Hello shellnewuser,

Could you please try following.

awk 'BEGIN{OFS=","} /\*\*\*END\*\*\*/{if(val){print val};print;val="";next} /^abc/{print;next} {val=(val?val OFS:"")$0}'  Input_file

Thanks,
R. Singh

Thank you very much RavinderSingh13 for such a prompt response. The code did work but I have following issues:

  1. The code did throw the expected output on the terminal but what I need is that the Input file itself is modified to have the data between "abc" and "***END***" transposed in rows as shown on the terminal.
  2. Also, in some files the number of "" around the word "END" varies, is there a way to generalize the number of "" so I don't have to worry about it?
  3. There is some additional data before "abc" which does not need to be transposed, how do I keep that data as it is along with transposing the data between "abc" and "***END***" like you did.

If its not too much to ask, could you kindly mention what each piece of your code does so I can learn as well.

Once again thank you for help!

Regards.