次の変数を含むテキストファイル "users.txt"があります。
trialuser1,paidUser1,paidPasswd1,paidDate1
trialuser2,paidUser2,paidPasswd2,paidDate2
trialuser3,paidUser3,paidPasswd3,paidDate3
trialuser4,paidUser4,paidPasswd4,paidDate4
trialuser5,paidUser5,paidPasswd5,paidDate5
....
....
....
次に、上記のテキストファイルに格納されている変数を使用して、「更新」SQLステートメントを含むSQL「updateusers.sql」ファイルを生成するシェルスクリプトを作成したいと思います。
update systemx.thetable set username='paidUser1',password='paidPasswd1',payment='paid',paidDate='paidDate1',token='init' where username='trialuser1';
update systemx.thetable set username='paidUser2',password='paidPasswd2',payment='paid',paidDate='paidDate2',token='init' where username='trialuser2';
update systemx.thetable set username='paidUser3',password='paidPasswd3',payment='paid',paidDate='paidDate3',token='init' where username='trialuser3';
update systemx.thetable set username='paidUser4',password='paidPasswd4',payment='paid',paidDate='paidDate4',token='init' where username='trialuser4';
update systemx.thetable set username='paidUser5',password='paidPasswd5',payment='paid',paidDate='paidDate5',token='init' where username='trialuser5';
....
....
....
ベストアンサー1
#!/usr/bin/awk -f
BEGIN {
FS=",";
fmt="update systemx.thetable set username='%s'," \
"password='%s',payment='paid',paidDate='%s'," \
"token='init' where username='%s';\n";
};
{ printf fmt, $2, $3, $4, $1 };
たとえば、別の名前で保存するには、次のようにsamin.awk
実行可能にしますchmod +x samin.awk
。
$ ./samin.awk users.txt
update systemx.thetable set username='paidUser1',password='paidPasswd1',payment='paid',paidDate='paidDate1',token='init' where username='trialuser1';
update systemx.thetable set username='paidUser2',password='paidPasswd2',payment='paid',paidDate='paidDate2',token='init' where username='trialuser2';
update systemx.thetable set username='paidUser3',password='paidPasswd3',payment='paid',paidDate='paidDate3',token='init' where username='trialuser3';
update systemx.thetable set username='paidUser4',password='paidPasswd4',payment='paid',paidDate='paidDate4',token='init' where username='trialuser4';
update systemx.thetable set username='paidUser5',password='paidPasswd5',payment='paid',paidDate='paidDate5',token='init' where username='trialuser5';