split a single sql file into multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers split a single sql file into multiple files
# 1  
Old 09-11-2008
split a single sql file into multiple files

Hi,I have a single sql file containing many create table ddl's.Example:
CREATE TABLE sec_afs
(
rpt_per_typ_c char(1) NOT NULL,
rpt_per_typ_t varchar(20) NULL,
LOCK ALLPAGES
go
EXEC sp_primarykey 'sec_afs', rpt_per_typ_c
go
GRANT SELECT ON sec_afs TO developer_read_only
go

CREATE TABLE dbo.sec_iccc
(
user_nt_id_c char(16) NOT NULL,
unit_id_c char(4) NOT NULL
)
LOCK ALLPAGES
go
GRANT SELECT ON sec_iccc TO developer_read_only
go

CREATE TABLE sac_recon(
rec_number int NOT NULL,
rec_grp_number int NOT NULL,
)
go

I want to split this file into separate files-one each for a table,i have a blank line before every "create table"statement.So may be I can create a new file once every "create table" is encountered.Please tell me how to do this in unix.-Thanks
# 2  
Old 09-11-2008
If you have csplit try
Code:
csplit mynewfile myfile.sql '^$'

# 3  
Old 09-12-2008
split a single sql file into multiple files

Hi Jim,Thanks for replying,
I tried the following:
$ csplit all_tables_Production '^$'
csplit: ^$: bad line number
$ csplit all_tables_Production %^$%
csplit: %^$ - out of range
$ csplit all_tables_Production `^$`
ksh: ^$: not found
but it doesn't work.
# 4  
Old 09-12-2008
If the blank line between CREATE TABLE paragraphs is always present this should suffice:

Code:
awk '/CREATE TABLE/{ n++; print > (f="out_" n); close(f)}' RS=  sql_file

Otherwise use,

Code:
awk '/CREATE TABLE/{f=0 ;n++; print >(file="out_" n); close("out_" n-1)} f{ print > file}; /CREATE TABLE/{f=1}'  sql_file

This User Gave Thanks to rubin For This Post:
# 5  
Old 09-15-2008
split a single sql file into multiple files

Hi Rubin,
Thanks your solution works!
awk '/CREATE TABLE/{ n++; print > (f="out_" n); close(f)}' RS= alltables
but,i have close to 183 "create table" scripts in my file and the above crashes out with the error:
awk: too many output files 10
record number 11

The second solution GIVES:
awk '/CREATE TABLE/{f=0 ;n++; print >(file="out_" n); close("out_" n-1)} f{ print > file}; /CREATE TABLE/{f=1}' alltables
awk: syntax error near line 1
awk: bailing out near line 1


Please let me know -how can we increase the size from 10 to 200.
Thanks
# 6  
Old 09-15-2008
I see ..., that's not an awk issue, but an OS one. I doubt though that the limit of files in one dir is only 10, it's got to be way more than that.
I tested the codes in Solaris, and they bombed out only after ~ 55000 files were created in one dir, they were all in good shape,and my test file had ~1,500,000 lines.

Well in this case I'd suggest to split the file in smaller chunks ( see man split pages of your OS ) to the size that the codes would not fail, create the same number of directories as the number of chunks created, move these chunks to the newly created dirs, and run the given codes separately in each of these directories created.

BTW if you're on Solaris use nawk.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split a single file into multiple files based on a value.

Hi All, I have the sales_data.csv file in the directory as below. SDDCCR; SOM ; MD6546474777 ;05-JAN-16 ABC ; KIRAN ; CB789 ;04-JAN-16 ABC ; RAMANA; KS566767477747 ;06-JAN-16 ABC ; KAMESH; A33535335 ;04-JAN-16 SDDCCR; DINESH; GD6674474747 ;08-JAN-16... (4 Replies)
Discussion started by: ROCK_PLSQL
4 Replies

2. UNIX for Dummies Questions & Answers

Split files into smaller ones with 1000 hierarchies in a single file.

input file: AD,00,--,---,---,---,---,---,---,--,--,--- AM,000,---,---,---,---,---,--- AR, ,---,--,---,--- AA,---,---,---,--- AT,--- AU,---,---,--- AS,---,--- AP,---,---,--- AI,--- AD,00,---,---,---, ,---,---,---,---,---,--- AM,000,---,---,--- AR,... (6 Replies)
Discussion started by: kcdg859
6 Replies

3. Shell Programming and Scripting

Split single file into multiple files using pattern matching

I have one single shown below and I need to break each ST|850 & SE to separate file using unix script. Below example should create 3 files. We can use ST & SE to filter as these field names will remain same. Please advice with the unix code. ST|850 BEG|PO|1234 LIN|1|23 SE|4 ST|850... (3 Replies)
Discussion started by: prasadm
3 Replies

4. UNIX for Dummies Questions & Answers

Split single file into n number of files

Hi, I am new to unix. we have a requirement here to split a single file into multiples files based on the number of people available for processing. So i tried my hand at writing some code as below. #!/bin/bash var1=`wc -l $filename` var2=$var1/$splitno split -l $var2 $1 Please help me... (6 Replies)
Discussion started by: quirkguy
6 Replies

5. Shell Programming and Scripting

Execute multiple SQL scripts from single SQL Plus connection

Hi! I would like to do a single connection to sqlplus and execute some querys. Actually I do for every query one connection to database i.e echo 'select STATUS from v$instance; exit' > $SQL_FILE sqlplus user/pass@sid @$SQL_FILE > $SELECT_RESULT echo 'select VERSION from v$instance;... (6 Replies)
Discussion started by: guif
6 Replies

6. Shell Programming and Scripting

Split the single file lines into multiple files

Let's assume that I have a file name called ‘A' and it has 100 lines in it and would like to split these 100 lines into 4 files as specified bellow. INPUT: Input file name A 1 2 3 4 5 6 7 8 9 ........100 Output: 4 output files (x,y,z,w) File x should contains (Skip 4 lines)... (15 Replies)
Discussion started by: subbarao25
15 Replies

7. Shell Programming and Scripting

Split single file into multiple files based on the number in the column

Dear All, I would like to split a file of the following format into multiple files based on the number in the 6th column (numbers 1, 2, 3...): ATOM 1 N GLY A 1 -3.198 27.537 -5.958 1.00 0.00 N ATOM 2 CA GLY A 1 -2.199 28.399 -6.617 1.00 0.00 ... (3 Replies)
Discussion started by: tomasl
3 Replies

8. Programming

Single sql query to spool to multiple files

Is there anyway to spool my select statement into spool files of max 10000 records each? eg I have a select statement that will return 45000 records. A normal spool command will output the 45000 into just one spool file. How can I make sqlplus do this? 00001 - 10000 records --- spool... (3 Replies)
Discussion started by: Leion
3 Replies

9. Shell Programming and Scripting

Split a file into multiple files

I have a file ehich has multiple create statements as create abc 123 one two create xyz 456 four five create nnn 666 six four I want to separte each create statement in seperate files (3 Replies)
Discussion started by: glamo_2312
3 Replies

10. Shell Programming and Scripting

Executing Multiple .SQL Files from Single Shell Script file

Hi, Please help me out. I have around 700 sql files to execute in a defined order, how can i do it from shell script (3 Replies)
Discussion started by: anushilrai
3 Replies
Login or Register to Ask a Question