2011-11-25 89 views
2

myfile.txt的文件的內容如下:在AWK打印是

| dbname     | 
| dbname1    | 
| dbname2    | 

以下命令預計將產生的輸出如下所示:

cat myfile.txt | awk '{print "mysql -uroot -Bse \"call mysql.p_check_fk_constraint_violations('\'$2\'','\''%'\'')\""}' 

的預期輸出:

mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname','%')" 
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname1','%')" 
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname2','%')" 

但是實際輸出是:

mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('','%')" 

如何在awk語句中添加數據庫名稱?

回答

2

這應該做到這一點 -

[jaypal~/Temp]$ cat db.file 
| dbname     | 
| dbname1    | 
| dbname2    | 

在這裏,我們與您的文本替換第二個字段,並使用「&」相匹配的領域越來越取代。

[jaypal~/Temp]$ awk -F\| '{sub($2,"mysql \-uroot \-Bse \"call mysql.p_check_fk_constraint_violations\(&,\%\)\""); print $2}' db.file 
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations(dbname     ,%)" 
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations(dbname1    ,%)" 
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations(dbname2    ,%)" 

或者作爲Teudimundo建議,你可以做 -

[jaypal~/Temp]$ cat db.file | awk '{print "mysql -uroot -Bse \"call mysql.p_check_fk_constraint_violations("$2",'%')\""}' 
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations(dbname,%)" 
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations(dbname1,%)" 
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations(dbname2,%)" 

UPDATE

[jaypal~/Temp]$ cat db.file | awk '{print "mysql -uroot -Bse \"call mysql.p_check_fk_constraint_violations('"'"'"$2"'"'"', '"'"'%'"'"')"}' 
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname', '%') 
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname1', '%') 
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname2', '%') 

[jaypal~/Temp]$ awk '{ print $2 }' db.file | awk '{sub($1,"mysql \-uroot \-Bse \"call mysql.p_check_fk_constraint_violations\('"'"'&'"'"','"'"'%'"'"'\)\""); print $0}' 
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname','%')" 
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname1','%')" 
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname2','%')" 
+0

關閉。但我期望('dbname','%')「 – shantanuo

+0

檢查更新後的答案 –

1

here:'" $2 "'您正在關閉第一個awk ' char,所以「$ 2」由shell解釋。

1

您正在運行到,因爲單引號的問題,因爲Teudimundo說。爲了解決這個問題,就需要更換每個單引號',你想這個'"'"'嵌入,給這個awk命令:

awk '{print "mysql -uroot -Bse \"call mysql.p_check_fk_constraint_violations('"'"'$2'"'"', '"'"'%'"'"')"}' 

這工作,因爲'"'"'第一端的單引號的字符串爲AWK命令開始一個包含單引號的新雙引號字符串,然後用awk命令的其餘部分啓動一個新的單引號字符串。由於相鄰的字符串在外殼中連接,這種奇怪的外觀方法會產生您需要的字符串。

+0

它爲所有3行打印$ 2。不會替代變量 – shantanuo

+0

它只需要添加另一層雙引號。 –

0

它假設,這將是更好的使用MySQL的程序,而不是進出跳躍的使用mysql的腳本... 我不熟悉mysql的的程序語言,但我敢肯定,如果你搜索互聯網 你可以很快想出一個簡單的程序,像這樣:

delimiter // 
drop procedure run_proc // 

create procedure run_proc() 
begin 
    declare done boolean default 0; 
    declare l_db_name varchar(100); 
    declare cur_db_names cursor 
    for 
    select 
     schema_name 
    from 
     information_schema.schemata; 
    declare continue handler for 
    sqlstate '02000' set done=1; 

    open cur_db_names; 
    repeat 
    fetch cur_db_names into l_db_name; 
    call mysql.p_check_fk_constraint_violations(l_db_name,'%'); 
    until done end repeat; 
    close cur_db_names; 
end; 
// 

delimiter ; 

call run_proc;