The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 07:09 PM
how to divide single large log file into multiple files. kamleshm Shell Programming and Scripting 1 01-15-2008 07:33 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-11-2008
smarter_aries smarter_aries is offline
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
  #2 (permalink)  
Old 09-11-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
If you have csplit try
Code:
csplit mynewfile myfile.sql '^$'
  #3 (permalink)  
Old 09-12-2008
smarter_aries smarter_aries is offline
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.
  #4 (permalink)  
Old 09-12-2008
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
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
  #5 (permalink)  
Old 09-15-2008
smarter_aries smarter_aries is offline
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
  #6 (permalink)  
Old 09-15-2008
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
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.
Sponsored Links
Closed Thread

Bookmarks

Tags
multiple files, split, unix

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:31 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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