要读取的文件file.sql
包含以下文本
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;
我希望将三个查询存储在三个不同的文件中,如下所示
文件1.sql
create table temp
(name varchar(20), id number)
on commit reserve rows
文件2.sql
create table temp1
(name varchar(20), id number)
on commit reserve rows
文件3.sql
select name, id
from temp where id=21
使用 ksh 脚本同时保留空格
答案1
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