perl script to split the text file after every 4th field

I had a text file(comma seperated values) which contains as below

196237,ram,25-May-06,ram.kiran@xyz.com,204183,Pavan,4-Jun-07,Pavan.Desai@xyz.com,237107,ram Chandra,15-Mar-10,ram.krishna@xyz.com  ,237547,Ravi,6-Apr-10,ravi.gupta@xyz.com,245281,Dheeraj,6-Dec-10,Dheeraj_Turupu@xyz.com,257599,Venkata Maniteja,5-Dec-11,Venkat.alaga@xyz.com 

I require the text exactly as below(new line has to be inserted after the mail id i.e., every 4th field) as below

after running the script

196237,ram,25-May-06,ram.kiran@xyz.com,
204183,Pavan,4-Jun-07,Pavan.Desai@xyz.com,
237107,ram Chandra,15-Mar-10,ram.krishna@xyz.com  ,
237547,Ravi,6-Apr-10,ravi.gupta@xyz.com,
245281,Dheeraj,6-Dec-10,Dheeraj_Turupu@xyz.com,
257599,Venkata Maniteja,5-Dec-11,Venkat.alaga@xyz.com

How to write a perl script to format the raw text file as mentioned above ?

Could you please provide your suggestions and ideas on the above ?

Thanks in advance....

Regards,
Giri

$ perl -F, -lane 'foreach(@F){$i++;if($i%4!=0){printf("%s,",$_)}else{print $_ .","}}' input.txt
196237,ram,25-May-06,ram.kiran@xyz.com,
204183,Pavan,4-Jun-07,Pavan.Desai@xyz.com,
237107,ram Chandra,15-Mar-10,ram.krishna@xyz.com  ,
237547,Ravi,6-Apr-10,ravi.gupta@xyz.com,
245281,Dheeraj,6-Dec-10,Dheeraj_Turupu@xyz.com,
257599,Venkata Maniteja,5-Dec-11,Venkat.alaga@xyz.com,

1 Like

Thanks alot kamaraj...

It is working in unix environment.

But I am writing the piece of code in Windows environment so Could you please provide the perl script to run in windows...

Thank you once again.....

Regards,
Giri

this should work in windows ( not tried )

$ perl -F, -lane "foreach(@F){$i++;if($i%4!=0){printf(\"%s,\",$_)}else{print $_ .\",\"}}" input.txt