SQLファイルから一度に1つのクエリを抽出する方法は?

SQLファイルから一度に1つのクエリを抽出する方法は?

SQLファイルから一度に1つのクエリを抽出しようとしています。

私が試したことは次のとおりです。

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ファイルは次のとおりです。

SQLファイル.sql

test1で*を選択します。

test2で*を選択します。

テスト 3 で * を選択します。

私は次のような結果を得ます。

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;
;

ベストアンサー1

あなたが何をしているのか100%確信することはできませんが、推測してみましょう。

.sqlファイルのテストケースでは、名前を次のように指定しましたtest.sql

select * from test1;
select * from test2;
select * from test3;

そして、SQLデータを読み込んでエコーするスクリプトは次のとおりです。

#!/bin/sh

# Get the raw sql data
RAW=`cat test.sql`

# We want to split the raw sql on a line return.
IFS="
"

# Echo out each line? Not sure if this is what you want.
for sql in $RAW
do
    echo "SQL line: [${sql}]"
done

exit

これにより、次のような結果が表示されます。

SQL line: [select * from test1;]
SQL line: [select * from test2;]
SQL line: [select * from test3;]

私の考えにあなたが逃している部分はまさにそのIFS部分です。からman sh

IFS  Input Field Separators. This is normally set to ⟨space⟩, 
     ⟨tab⟩, and ⟨newline⟩.

おすすめ記事