2011-11-25 90 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;