сценарий оболочки для выполнения команд sql «обновления» из списка переменных, хранящихся в текстовом файле

У меня есть текстовый файл "users.txt", который содержит переменные, как показано ниже:

trialuser1,paidUser1,paidPasswd1,paidDate1
trialuser2,paidUser2,paidPasswd2,paidDate2
trialuser3,paidUser3,paidPasswd3,paidDate3
trialuser4,paidUser4,paidPasswd4,paidDate4
trialuser5,paidUser5,paidPasswd5,paidDate5
....
....
....

Теперь я хочу создать сценарий оболочки, который создаст файл sql "updateusers.sql", содержащий "Обновить" 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
18.08.2017, 07:43
3 ответа
#!/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';
3
28.01.2020, 05:06

awkдействительно хороший выбор для обработки файлов построчно и их преобразования; вот как я это придумал:

awk -F, '{print "update systemx.thetable set username='\''"$2"'\'',password='\''"$3"'\'',payment='\''paid'\'',paidDate='\''"$4"'\'',token='\''init'\'' where username='\''"$1"'\'';"}' users.txt

где я предположил, что вам нужно поле 4, «paidDate5», вместо статической даты «2017-08-17».

Это напечатает соответствующий текст на экране; вы можете перенаправить его в сценарий sql для последующего выполнения.

1
28.01.2020, 05:06

awkрешение:

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';
1
28.01.2020, 05:06

Теги

Похожие вопросы