The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Split a file into multiple files glamo_2312 Shell Programming and Scripting 3 09-15-2008 09:58 AM
Split file into multiple files depending upon first 4 digits deepakgang Shell Programming and Scripting 4 04-09-2008 01:21 AM
Executing Multiple .SQL Files from Single Shell Script file anushilrai Shell Programming and Scripting 3 04-07-2008 10:09 AM
Help Needed : Split one big file to multiple files monicasgupta Shell Programming and Scripting 5 03-03-2008 06:09 PM
how to divide single large log file into multiple files. kamleshm Shell Programming and Scripting 1 01-15-2008 06:33 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-11-2008
Registered User
 

Join Date: Sep 2008
Posts: 4
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
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-11-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 5,214
If you have csplit try
Code:
csplit mynewfile myfile.sql '^$'
Reply With Quote
  #3 (permalink)  
Old 09-12-2008
Registered User
 

Join Date: Sep 2008
Posts: 4
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.
Reply With Quote
  #4 (permalink)  
Old 09-12-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 297
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
Reply With Quote
  #5 (permalink)  
Old 09-15-2008
Registered User
 

Join Date: Sep 2008
Posts: 4
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
Reply With Quote
  #6 (permalink)  
Old 09-15-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 297
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Bookmarks

Tags
multiple files, split, unix

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:




All times are GMT -4. The time now is 05:53 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66