Split records into multiple records

Hi All,

I am trying to split a record into multiple records based on a value.

Input.txt

"A",1,0,10
"B",2,0,10,15,20
"C",3,11,14,16,19,21,23
"D",1,0,5

My desired output is:

"A",1,0,10
"B",2,0,10
"B",2,15,20
"C",3,11,14
"C",3,16,19
"C",3,21,23
"D",1,0,5

I tried this code could you please assist me:

awk 'BEGIN {RS=ORS=","}
  (NR > 1) && ($2>1) { ORS="\n"; print ""; ORS="," }
  1
' Input.txt

Thanks in advacne.

---------- Post updated at 04:02 PM ---------- Previous update was at 12:55 PM ----------

I got some solution, need some modification:

$ awk -F',' '{for (i =2; ++i <= NF;) print $1 "," $2 "," $i}' H_Input.txt

For the above command I am getting below output:

"A",1,0
"A",1,10
"B",2,0
"B",2,10
"B",2,15
"B",2,20
"C",3,11
"C",3,14
"C",3,16
"C",3,19
"C",3,21
"C",3,23
"D",1,0
"D",1,5

but desired output is:

"A",1,0,10
"B",2,0,10
"B",2,15,20
"C",3,11,14
"C",3,16,19
"C",3,21,23
"D",1,0,5

Just add another column and try..

awk -F',' '{for (i =2; ++i <= NF; ) print $1 "," $2 "," $i "," $++i}' H_Input.txt
awk -F, '{for (i=3;i<=NF;i+=2) print $1, $2, $i, $(i+1)}' OFS=, infile

Thanks all its working fine

you can try justdoit code for more complex structures,
for exa you want to start=1 and last=3 column(its fixed between the first and 3.column) and incremantal column count=2 and then you can try this :wink:

# awk -F, -vs=1 -vl=3 -vinc=2 'function justdoit(j,k,cc)
{for(i=j;i<k;i++)printf "%s%c",a,FS;if(cc==1){printf "%s",a[k]} else printf "%s%c",a[k],FS}
{t=NF;c=1;if(t<=l+inc)print;else{split($0,a);if(inc==1)t=t-1;while(t>inc){if(c==1){justdoit(s,l+inc,1);n=s+l;c++;}
else{justdoit(s,l);;rn=n+inc-s;while(!a[rn])rn-=s;justdoit(n,rn,1);};t-=inc;n+=inc;;printf "%s","\n";}}}' infile
"A",1,0,10
"B",2,0,10,15
"B",2,0,20
"C",3,11,14,16
"C",3,11,19,21
"C",3,11,23
"D",1,0,5

regards
ygemici