Splitting single file into n files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting single file into n files
# 1  
Old 11-02-2011
Splitting single file into n files

Hi all,

I am new to scripting and I have a requirement
we have source file as
Code:
HEADER 01.10.2010 14:32:37 NAYA
TA0022
TA0000
20000001;20060612;99991231;K4;02;3
20000008;20080624;99991231;K4;02;3
20000026;19840724;99991231;KK;01;3
20000027;19840724;99991231;KK;01;3
20000029;20100730;99991231;K4;02;3
20000035;19841130;99991231;K4;02;0
20000036;19801101;99991231;K3;02;3
20000039;19800901;99991231;K5;02;0
20000040;19840724;99991231;KK;01;3
20000042;19840901;99991231;K5;01;0
20000053;19800101;99991231;K1;03;3
20000058;19841101;99991231;K5;10;0
20000081;20100901;99991231;K8;08;3
20000095;20090414;99991231;K4;02;3
20000099;19840724;99991231;KK;01;3
TA0035
TA0002
;;;;;;;19800421;K011121;99991231;55580420;20000001;ram;kumar;;1;;55580420;;ON;E;19845000173;;
;;;;;;;19800421;K012221;99991231;66630312;20000008;Langdevlt;Håvard;;1;;66630312;;ON;E;12000022116;;
;;;;;;;19800421;K013331;99991231;66610730;20000026;Nero;Tt;;1;;66610730;;ON;E;30000039101;;
;;;;;;;19800421;K013331;99991231;55560806;20000027;Mahi;Sub;;1;;55560806;;ON;E;06085644902;;
;;;;;;;19800421;K011111;99991231;66680517;20000029;Oy;Janu;;1;;66680517;;ON;E;17056836799;;
;;;;;;;19840727;N344440;99991231;66680825;20000035;Neha;Mitra;;2;;66680825;;ON;E;25000836842;;
;;;;;;;19801128;AC00006;99991231;19801128;20000036;Fisces;mitra;;2;;77700201;;ON;E;01027032873;;
;;;;;;;19800421;K011111;99991231;66670105;20000039;Gaya;Bondi;;2;;66670105;;ON;E;0500031117;;
;;;;;;;19800421;K012341;99991231;66630922;20000040;Peter;nikil;;2;;66630922;;ON;E;11096388094;;
;;;;;;;19840727;N345580;99991231;66661125;20000042;Amale;Sabji;;2;;66661125;;ON;E;25116632473;;
;;;;;;;19800427;AC51056;99991231;86340216;20000053;Swapna;Oddu;;1;;86340216;;ON;E;16025559526;;
;;;;;;;19840727;N323480;99991231;66651213;20000058;Noot;Enna;;1;;66651213;;ON;E;13126546569;;
;;;;;;;19800421;K011121;99991231;77760813;20000081;Arya;Raj;;2;;77760813;;ON;E;13085553823;;
;;;;;;;19800421;K016621;99991231;55590430;20000095;Raj;Jarry;;1;;55590430;;ON;E;30333768168;;
;;;;;;;19800421;K016621;99991231;55580826;20000099;Larsen;Toubro;;1;;55580826;;ON;E;26011132119;;
TA0016
TA0006
00000;hello;bb789;iphone
00002;fehld;sm258;bb
00004;hallo;rt4569;ONkia
00050;felko;ut1256;ram
00050;felko;ar7890;sami
TA0086

Here record starting with 'TA' is the name of the tables, then followed by data with semicolon delimited.
Ex: TA0022 table has no data, TA0000 table has 15 records data and so on.

Now the requirement is to split the above source file into 7 different files i.e.table wise, TA0022,TA0000, TA0035,TA0002,TA0016,TA0006, TA0086(same names) with the associated data in it.

Here both the number of files as well as data are varied but the table name starts with 'TA'

Please help me to achieve this..

Thanks in advance.
SRK.

Last edited by srk409; 11-02-2011 at 05:10 AM..
# 2  
Old 11-02-2011
Code:
awk 'NR > 1 && /^TA/ {f=$1; printf "" > f; next} NR > 1 {print $0 >> f}' infile

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 11-02-2011
it not display anything.
# 4  
Old 11-02-2011
@muralikri, zaxxon code is perfectly working ..

If your OS is solaris use nawk ..
# 5  
Old 11-16-2011
Thanks zaxxon. Its working fine.
Now I got few modifications in source file.
Code:
HEADER 01.10.2010 14:32:37 NAYA
TA0022
TRP512
20000001;20060612;99991231;K4;02;3
20000008;20080624;99991231;K4;02;3
20000026;19840724;99991231;KK;01;3
20000027;19840724;99991231;KK;01;3
20000029;20100730;99991231;K4;02;3
20000035;19841130;99991231;K4;02;0
20000036;19801101;99991231;K3;02;3
20000039;19800901;99991231;K5;02;0
20000040;19840724;99991231;KK;01;3
20000042;19840901;99991231;K5;01;0
20000053;19800101;99991231;K1;03;3
20000058;19841101;99991231;K5;10;0
20000081;20100901;99991231;K8;08;3
20000095;20090414;99991231;K4;02;3
20000099;19840724;99991231;KK;01;3
TA0035
CSKT
;;;;;;;19800421;K011121;99991231;55580420;20000001;ram;kumar;;1;;55580420;;ON;E;19845000173;;
;;;;;;;19800421;K012221;99991231;66630312;20000008;Langdevlt;Håvard;;1;;66630312;;ON;E;12000022116;;
;;;;;;;19800421;K013331;99991231;66610730;20000026;Nero;Tt;;1;;66610730;;ON;E;30000039101;;
;;;;;;;19800421;K013331;99991231;55560806;20000027;Mahi;Sub;;1;;55560806;;ON;E;06085644902;;
;;;;;;;19800421;K011111;99991231;66680517;20000029;Oy;Janu;;1;;66680517;;ON;E;17056836799;;
;;;;;;;19840727;N344440;99991231;66680825;20000035;Neha;Mitra;;2;;66680825;;ON;E;25000836842;;
;;;;;;;19801128;AC00006;99991231;19801128;20000036;Fisces;mitra;;2;;77700201;;ON;E;01027032873;;
;;;;;;;19800421;K011111;99991231;66670105;20000039;Gaya;Bondi;;2;;66670105;;ON;E;0500031117;;
;;;;;;;19800421;K012341;99991231;66630922;20000040;Peter;nikil;;2;;66630922;;ON;E;11096388094;;
;;;;;;;19840727;N345580;99991231;66661125;20000042;Amale;Sabji;;2;;66661125;;ON;E;25116632473;;
;;;;;;;19800427;AC51056;99991231;86340216;20000053;Swapna;Oddu;;1;;86340216;;ON;E;16025559526;;
;;;;;;;19840727;N323480;99991231;66651213;20000058;Noot;Enna;;1;;66651213;;ON;E;13126546569;;
;;;;;;;19800421;K011121;99991231;77760813;20000081;Arya;Raj;;2;;77760813;;ON;E;13085553823;;
;;;;;;;19800421;K016621;99991231;55590430;20000095;Raj;Jarry;;1;;55590430;;ON;E;30333768168;;
;;;;;;;19800421;K016621;99991231;55580826;20000099;Larsen;Toubro;;1;;55580826;;ON;E;26011132119;;
TA0016
CSKS
00000;hello;bb789;iphone
00002;fehld;sm258;bb
00004;hallo;rt4569;ONkia
00050;felko;ut1256;ram
00050;felko;ar7890;sami
TP0086

Modification is change in the file names.
Could you please help in this scenario?

Thanks
SRK
# 6  
Old 11-16-2011
I guess you mean
Code:
TA0022
TRP512
CSKT
TP0086
...

?

Code:
awk 'NR > 1 && /^TA|^TR|^TP|^CSKT/ {f=$1; printf "" > f; next} NR > 1 {print $0 >> f}' infile

You gotta read up on regular expressions ^^
If I forgot any file name identifier, it is up to you to complete them Smilie
This User Gave Thanks to zaxxon For This Post:
# 7  
Old 11-16-2011
Its working fine....Thanks a lotSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting a single xml file into multiple xml files

Hi, I'm having a xml file with multiple xml header. so i want to split the file into multiple files. Sample.xml consists multiple headers so how can we split these multiple headers into multiple files in unix. eg : <?xml version="1.0" encoding="UTF-8"?> <ml:individual... (3 Replies)
Discussion started by: Narendra921631
3 Replies

2. Shell Programming and Scripting

Paste 2 single column files to a single file

Hi, I have 2 csv/txt files with single columns. I am trying to merge them using paste, but its not working.. output3.csv: flowerbomb everlon-jewelry sofft steve-madden dolce-gabbana-watchoutput2.csv: http://www1.abc.com/cms/slp/2/Flowerbomb http://www1.abc.com/cms/slp/2/Everlon-Jewelry... (5 Replies)
Discussion started by: ajayakunuri
5 Replies

3. Shell Programming and Scripting

Execution of loop :Splitting a single file into multiple .dat file

hdr=$(cut -c1 $path$file|head -1)#extract header”H” trl=$(cut -c|path$file|tail -1)#extract trailer “T” SplitFile=$(cut -c 50-250 $path 1$newfile |sed'$/ *$//' head -1')# to trim white space and extract table name If; then # start loop if it is a header While read I #read file Do... (4 Replies)
Discussion started by: SwagatikaP1
4 Replies

4. Shell Programming and Scripting

Splitting a single file to multiple files

Hi Friends , Please guide me with the code to extract multiple files from one file . The File Looks like ( Suppose a file has 2 tables list ,column length may vary ) H..- > File Header.... H....- >Table 1 Header.... D....- > Table 1 Data.... T....- >Table 1 Trailer.... H..-> Table 2... (1 Reply)
Discussion started by: AspiringD
1 Replies

5. Shell Programming and Scripting

Help me pls : splitting single file in unix into different files based on data

I have a file in unix with sample data as follows : -------------------------------------------------------------- -------------------------------------------------------------- {30001002|XXparameter|Layout|$ I want this file to be splitted into different files and corresponding to the sample... (54 Replies)
Discussion started by: Ravindra Swan
54 Replies

6. Shell Programming and Scripting

Need help splitting huge single record file

I was given a data file that I need to split into multiple lines/records based on a key word. The problem is that it is 2.5GB or bigger and everything I try in perl or sed causes a Segmentation fault. Can someone give me some other ideas. The data is of the form:... (5 Replies)
Discussion started by: leolson
5 Replies

7. Shell Programming and Scripting

Splitting & reformating a single file

I have a bif text file with the following format: d1_03 fr:23 d1_03 fr:56 d1_03 fr:67 d1_03 fr:78 d1_01 fr:35 d1_01 fr:29 d1_01 fr:45 d2_09 fr:34 d2_09 fr:78 d3_98 fr:90 d3_98 fr:104 d3_98 fr:360 I have like thousands of such lines I want to reformat this file based on column 1... (3 Replies)
Discussion started by: Lucky Ali
3 Replies

8. UNIX for Advanced & Expert Users

Splitting the single csv file

Hi, I have a requiement where in i will get a single file but there will be mutiple headers. Suppose say for eg: Header1 Data... Data... Header2 Data.. Data.. Header3 Data.. Data.. I want to split each with the corresponding data into a single file. Please let me know how... (1 Reply)
Discussion started by: weknowd
1 Replies

9. Shell Programming and Scripting

Splitting file into 2 files ?

Hi extending to one of my previous posted query .... I am using nawk -v invar1="$aa" '{print > ("ABS\_"((/\|/)?"A\_":"B\_")invar1"\_NETWORKID.txt")}' spfile.txt to get 2 different files based on split condition i.e. "|" Similar to invar1 variable in nawk I also need one more variable... (18 Replies)
Discussion started by: shekharjchandra
18 Replies

10. Shell Programming and Scripting

Splitting files from one file

Hi, I have an input file like: 111 abcdefgh asdfghjk dfghjkl 222 aaaaaaa bbbbbb 333 djfhfgjktitjhgfkg 444 djdhfjkhfjkghjkfg hsbfjksdbhjkgherjklg fjkhfjklsahjgh fkrjkgnj I want to read this input file and make separate output files with the header as numric value like "111"... (9 Replies)
Discussion started by: saltysumi
9 Replies
Login or Register to Ask a Question