Split records into multiple records


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split records into multiple records
# 1  
Old 03-06-2012
Split records into multiple records

Hi All,

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

Input.txt
Code:
"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:
Code:
"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:
Code:
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:

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

For the above command I am getting below output:
Code:
"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:
Code:
"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


Last edited by kmsekhar; 03-06-2012 at 05:16 AM..
# 2  
Old 03-06-2012
Just add another column and try..
Code:
awk -F',' '{for (i =2; ++i <= NF; ) print $1 "," $2 "," $i "," $++i}' H_Input.txt

# 3  
Old 03-06-2012
Code:
awk -F, '{for (i=3;i<=NF;i+=2) print $1, $2, $i, $(i+1)}' OFS=, infile

# 4  
Old 03-06-2012
Thanks all its working fine
# 5  
Old 03-06-2012
Quote:
Originally Posted by kmsekhar
Hi All,

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

Input.txt
Code:
"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:
Code:
"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:
Code:
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:

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

For the above command I am getting below output:
Code:
"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:
Code:
"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

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 Smilie
Code:
# awk -F, -vs=1 -vl=3 -vinc=2 'function justdoit(j,k,cc)
{for(i=j;i<k;i++)printf "%s%c",a[i],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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to split one record to multiple records?

Hi, I have one tab delimited file which is having multiple store_ids in first column seprated by pipe.I want to split the file on the basis of store_id(separating 1st record in to 2 records ). I tried some more options like below with using split,awk etc ,But not able to get proper output. can... (1 Reply)
Discussion started by: jaggy
1 Replies

2. Shell Programming and Scripting

Split records

Hi I have a file $cat test a,1;2;3 b,4;5;6;7 c,8;9 I want to split each record to multiple based on semicolon in 2nd field. i.e a,1 a,2 a,3 b,4 b,5 (3 Replies)
Discussion started by: Shivdatta
3 Replies

3. Shell Programming and Scripting

Split Records

I have a flat file with 2 columns Id,loc 1,nj:ny:pa 2,pa 3,ca:tx:fl:nj Second colum data is seperated by semi colon and can i have many locations for one id Output i need is 1,nj 1,ny 1,pa 1,pa 3,ca 3,tx 3,fl (1 Reply)
Discussion started by: traininfa
1 Replies

4. UNIX for Dummies Questions & Answers

Split single record to multiple records

Hi Friends, source .... col1,col2,col3 a,b,1;2;3 here colom delimeter is comma(,). here we dont know what is the max length of col3 means now we have 1;2;3 next time i will receive 1;2;3;4;5;etc... required output .............. col1,col2,col3 a,b,1 a,b,2 a,b,3 please give me... (5 Replies)
Discussion started by: bab.galary
5 Replies

5. Shell Programming and Scripting

split records into different files

Hi All, I want my file to be split based on value of 'N' (passed as argument). If value of 'N' is '2' then 4 new files will be generated from the below source file and the o/p file shoud look like File_$num , where num will be incremental. Source file: 1 2 3 4 5 O/p Files: ... (6 Replies)
Discussion started by: HemaV
6 Replies

6. Shell Programming and Scripting

Split a single record to multiple records & add folder name to each line

Hi Gurus, I need to cut single record in the file(asdf) to multile records based on the number of bytes..(44 characters). So every record will have 44 characters. All the records should be in the same file..to each of these lines I need to add the folder(<date>) name. I have a dir. in which... (20 Replies)
Discussion started by: ram2581
20 Replies

7. UNIX for Advanced & Expert Users

Split records based on '-'

HI, I have a pipe delimiter file , I have to search for second field pattern, if the second field does not contain a '-' , I need to start capturing the record from this line till I find another second field with '-' value. Below is the sample data SOURCE DATA ABC|ABC_702148-PARAM... (3 Replies)
Discussion started by: mora
3 Replies

8. UNIX for Dummies Questions & Answers

How to split multiple records file in n files

Hello, Each record has a lenght of 7 characters I have 2 types of records 010 and 011 There is no character of end of line. For example my file is like that : 010hello 010bonjour011both 011sisters I would like to have 2 files 010.txt (2 records) hello bonjour and ... (1 Reply)
Discussion started by: jeuffeu
1 Replies

9. Shell Programming and Scripting

Based on num of records in file1 need to check records in file2 to set some condns

Hi All, I have two files say file1 and file2. I want to check the number of records in file1 and if its atleast 2 (i.e., 2 or greater than 2 ) then I have to check records in file2 .If records in file2 is atleast 1 (i.e. if its not empty ) i have to set some conditions . Could you pls... (3 Replies)
Discussion started by: mavesum
3 Replies

10. Shell Programming and Scripting

dynamically split the records

Hi, I was wandering would it be possible to split the record dynamically based on the certain values, for an instance i have a file with record with predefined split value i.e 10 col1 col2 col3 col4 ------------------------ aaaa bbbb 2 44aaaabbbb55cccddd1110 mmn xnmn 3... (6 Replies)
Discussion started by: braindrain
6 Replies
Login or Register to Ask a Question