【发布时间】:2017-04-22 00:33:56
【问题描述】:
我正在尝试从 SQL 文件中一次提取一个查询。
这是我尝试过的
index1=1
index2=1
while read -n1 char; do
if [[ $char == ";" ]]
then
SUBSTRING=$(awk 'substr($index1,$index2)' sql1Temp.sql)
echo $SUBSTRING
index1=$index2
fi
((index2+=1))
done <sql1Temp.sql
我的 SQL 文件如下所示:
sql1Temp.sql
从 test1 中选择 *;
从 test2 中选择 *;
从 test3 中选择 *;
结果我得到了这个:
wedtorque@wedtorque-VirtualBox:~/Desktop$ ./masterFile.sh
select *from test1; select *from test2; select *from test3;
select *from test1; select *from test2; select *from test3;
select *from test1; select *from test2; select *from test3;
wedtorque@wedtorque-VirtualBox:~/Desktop$
而我期待这样的事情:
wedtorque@wedtorque-VirtualBox:~/Desktop$ ./masterFile.sh
select *from test1;
select *from test1;
select *from test1;
wedtorque@wedtorque-VirtualBox:~/Desktop$
此外,当我在 while 循环中回显 $char 时,它会在每次 $char 从查询中获取 * 时打印文件名,select *from test1;等等
类似的东西
wedtorque@wedtorque-VirtualBox:~/Desktop$ ./masterFile.sh
s
e
l
e
c
t
masterFile.sh sql1result.sql sql1.sql sql1Temp.sql sql2.sql Untitled Document
f
r
o
m
t
e
s
t
1
select *from test1; select *from test2; select *from test3;
;
same thing 3 times
我认为 awk 有什么问题?
【问题讨论】:
-
您能否解释一下您的脚本的用途,按照我的阅读方式,它似乎是一种类似于
cat sql1Temp.sql的复杂而缓慢的方式,我想一定有什么我失踪了。 -
我需要从 sql1Temp.sql 文件中获取一个 sql 查询,执行它然后将此查询复制到一个新文件并从原始文件中删除该查询。
-
每行是否始终只有一条 SQL 语句,或者您是否需要处理可能有多个 SQL 语句(用分号分隔)的情况?
-
我需要用 ; 分隔.我的sql文件包含多行查询以分号结尾。下一个查询以新行开始