0

The file to read is file.sql containing following text

create table temp 
(name varchar(20), id number)  
on commit reserve rows;

create table temp1  
(name varchar(20), id number) 
on commit reserve rows;

select name, id 
from temp where id=21;

I want the three queries stored in three different files as below

file1.sql

create table temp  
(name varchar(20), id number)  
on commit reserve rows 

file2.sql

create table temp1 
(name varchar(20), id number)  
on commit reserve rows

file3.sql

select name, id 
from temp where id=21

using ksh scripting while retaining whitespaces

  • It should be trivial to split on empty lines with awk... – don_crissti Mar 24 '17 at 13:43
0
re='create table'
csplit -s -k -f file. yourSqlFile "%^$re%" "/^$re/" '/^select name,/' '/./'
for f in file.[0][0-3]; do
   k=${f#*.0}
   mv "$f" "file$k.sql"
done
for i in {2,1,0};do
   j=$((i + 1))
   mv "file$i.sql" "file$j.sql"
done
  • can you please explain the above script – anandsa Mar 27 '17 at 6:41
  • csplit splits files on context which is provided by arguments to csplit, in particular, "%...%", "/.../" For more info do a man csplit – user218374 Mar 27 '17 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.