다음과 같은 변수가 포함된 텍스트 파일 "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
한 번에 한 줄씩 파일을 처리하고 어떤 방식으로든 변환하는 데 정말 좋은 옵션입니다.
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';