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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl script to split the text file after every 4th field
# 1  
Old 08-06-2012
perl script to split the text file after every 4th field

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

Code:
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

Code:
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

Last edited by zaxxon; 08-06-2012 at 12:02 PM.. Reason: code tags
# 2  
Old 08-06-2012
Code:
$ 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,

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 08-06-2012
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
# 4  
Old 08-06-2012
this should work in windows ( not tried )

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modification of perl script to split a large file into chunks of 5000 chracters

I have a perl script which splits a large file into chunks.The script is given below use strict; use warnings; open (FH, "<monolingual.txt") or die "Could not open source file. $!"; my $i = 0; while (1) { my $chunk; print "process part $i\n"; open(OUT, ">part$i.log") or die "Could... (4 Replies)
Discussion started by: gimley
4 Replies

2. UNIX for Dummies Questions & Answers

Split a file into parts only if the first field is different

Hi, I have a file like this: aaa 123 aaa 223 aaa 225 bbb 332 bbb 423 bbb 6755 bbb 324 ccc 112 ccc 234 ccc 897 Which I need to split into several files, something like split -l 3 but the way that the lines with the same names would only go into one file: (7 Replies)
Discussion started by: coppuca
7 Replies

3. Shell Programming and Scripting

Perl Split for text in file

Hi all I have written Perl script to swap the strings in the second a third column from a text file. My input file format is : the|empty|the|det lake|empty|lake|conj_and was|empty|was|auxpass drained|empty|drained|conj_and birds|empty|bird|s|nn The expected output file format is... (11 Replies)
Discussion started by: my_Perl
11 Replies

4. Shell Programming and Scripting

how to get split output of a file, using perl script

Hi, I have file: data.log.1 ### s1 main.build.3495 main.build.199 main.build.3408 ###s2 main.build.3495 main.build.3408 main.build.199 I want to read this file and store in two arrays in Perl. I have following command, which is working fine on command prompt. perl -n -e... (1 Reply)
Discussion started by: ashvini
1 Replies

5. Shell Programming and Scripting

awk script to split field data

Hi Experts, I have a Input.txt document which contains data fields seperated by tabs. There are 4 fields totally Named UNIQUE, ORDER, CONTACT and WINS. The UNIQUE field contains unique ID and the CONTACT field contains data seperated by comma in some records. I am looking to write an awk script... (12 Replies)
Discussion started by: forumthreads
12 Replies

6. Shell Programming and Scripting

Split file based on field

Hi I have a large file 2.6 million records and I am trying to split the file based on last column. I am doing awk -F"|" '{ print > $NF }' filename1 After around 1000 splits it gives me a error awk: can't open file 3332332423 input record number 1068, file filename1 source... (6 Replies)
Discussion started by: s_adu
6 Replies

7. Shell Programming and Scripting

Perl script to load text file into DB field

Hello, maybe this post is offtopic, sorry for the inconveniencies (maybe should be in the forum with questions about C, C++, Java, SQL...). I found examples about how to populate different fields from a text file (with MySQL, the LOAD DATA INFILE sentece), but not about how to load a complete... (2 Replies)
Discussion started by: aristegui
2 Replies

8. Shell Programming and Scripting

display file where 4th field = 200704

Hello I have a file which is pipe delimited. The 4 th field has value like 200704 or 200705 etc. Now i have to get only those records where 4th field is 200704 How can i do this? I have to get the whole record where 4th field = 200704 (7 Replies)
Discussion started by: vasuarjula
7 Replies

9. AIX

display file where 4th field = 200704

Hello I have a file which is pipe delimited. The 4 th field has value like 200704 or 200705 etc. Now i have to get only those records where 4th field is 200704 How can i do this? I have to get the whole record where 4th field = 200704 (4 Replies)
Discussion started by: vasuarjula
4 Replies

10. Shell Programming and Scripting

Split a field in awk script

Hi all, I have a field in the line, let's say argument $6, which is in the format 00.00 If i want to split the field to get rid of the "." in between of the amount, how can i do that i awk script? I have it like this split($6,a,".") but it will get rid of the last 2 digits after the... (4 Replies)
Discussion started by: CamTu
4 Replies
Login or Register to Ask a Question