次の変数を含むテキストファイル "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';
答え2
awk
一度に1行ずつファイルを処理し、何らかの方法で変換するのに本当に良いオプションです。
awk -F, '{print "update systemx.thetable set username='\''"$2"'\'',password='\''"$3"'\'',payment='\''paid'\'',paidDate='\''"$4"'\'',token='\''init'\'' where username='\''"$1"'\'';"}' users.txt
静的日付「2017-08-17」ではなく、フィールド4「paidDate5」が欲しいとします。
これにより、適切なテキストが画面に印刷されます。後で実行するためにSQLスクリプトにリダイレクトできます。
答え3
アッ解決策:
awk -F, '{ printf("update systemx.thetable set username=\047%s\047,password=\047%s\047,
payment=\047paid\047,paidDate=\047%s\047,token=\047init\047
where username=\047%s\047;\n",$2,$3,$4,$1) }' file
出力:
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';