Replace a string with multiple lines

Hello Guys,

I need to replace a string with multiple lines.

For eg:-

ABC,DEF,GHI,JKL,MNO,PQR,STU

need to convert the above as below:-

ABC,DEF,
GHI1
GHI2
GHI3,
JKL,MNO,
PQR1
PQR2
PQR3,
STU

i have tried using code as:-

echo "ABC,DEF,GHI,JKL,MNO,PQR,STU"| sed 's/GHI/GHI1\nGHI2\n/g'

it giving me as

ABC,DEF,GHI1nGHI2n,JKL,MNO,PQR,STU

thanks to help.

Regards,
Jaskirat

Hello Jassi,

Not sure about your logic but as per your output following may help you in same.

 awk -vone=1 -vtwo=2 -vthree=3 -F, '{for(i=1;i<=NF;i++){A=i%3==0?A ORS $i one ORS $i two ORS $i three ORS:A=A?A OFS $i:$i}} END{gsub(/^$/,X,A);gsub(/\n\,/,",\n",A);print A}'  OFS=,  Input_file
 

Output will be as follows.

ABC,DEF
GHI1
GHI2
GHI3,
JKL,MNO
PQR1
PQR2
PQR3,
STU
 

Hope this helps.

Thanks,
R. Singh

Standard sed does not understand \n in the replacement part, you need to use a hard newline instead:

$ echo "ABC,DEF,GHI,JKL,MNO,PQR,STU"| sed 's/GHI/\
GHI1\
GHI2\
GHI3/g'