Create new lines using a delimited string.

Hi

I have a text file called 'fileA' which contains the follwoing line examples

01:rec1:25,50,75,100
02:rec2:30,60
03:rec3:20,40

I would like to create a new file where each of the comma separated values appears on a new line but prefixed with the first two fields e.g.

01:rec1:25
01:rec1:50
01:rec1:75
01:rec1:100
02:rec2:30
02:rec2:60
03:rec3:20
03:rec3:40

I can get the delimited string appearing on a new line with

cat fileA.txt | sed 's/,/\n/g'
  • or
tr ',''\n'

But cannot prefix it with value from the first two fields?

Hi

$ awk -F '[:,]' '{for(i=3;i<=NF;i++){print $1,$2,$i;}}' OFS=":" file
01:rec1:25
01:rec1:50
01:rec1:75
01:rec1:100
02:rec2:30
02:rec2:60
03:rec3:20
03:rec3:40

Thanks guruprasadpr - that worked fine. Thank You!!!

awk -F: '{gsub(/,/,RS $1 FS $2 FS)}1' infile