一重引用符とコンマを追加するシェルスクリプト

一重引用符とコンマを追加するシェルスクリプト

次のファイルがありますinput.txt

$cat input.txt
This is sample
Input file
To execute sql statement

以下の出力を変数に割り当てる必要があります。

X=('This is sample', 'Input file', 'To execute sql statement')

これにより、上記の文字列Xを条件のSQLクエリへの入力として使用できますIN

select * from table where columnname in X

ここで私を助けてください。

ベストアンサー1

そしてbash

mapfile -t a < input.txt      # read input file into array a
printf -v x "'%s'," "${a[@]}" # add single quotes and commas, save result in variable x
printf -v query 'select * from table where columnname in (%s);' "${x:0:-1}" # strip the last comma from x
printf '%s\n' "$query"        # print query

出力:

select * from table where columnname in ('This is sample','Input file','To execute sql statement');

おすすめ記事