English 中文(简体)
输出数据库 INSERT 的 AWK 变量, 跳出单引数和反斜线
原标题:Outputting an AWK variable for database INSERT, escaping single quote and backslash

So I am running through a directory deriving names to insert into a MySQL database. Some of the names have a single quote ( ). I m using awk

ls *| awk -F=:  {print "INSERT into citation_sources (source_name) VALUES (" "   " $1 "   " ");"} 

While I can get the output to appear properly from awk. E.G.:

INSERT into citation_sources (source_name) VALUES ( Black s Law Dictionary, 2nd Edition (Black 1891));

当将数据插入 MySQL I 的时间时, d 想要避免做第二次数据编辑以正确避免使用 < code> 的单引号 。

我为我做这些 在我的腹部错过了什么?

问题回答

如果您没有古代的 ls , 请在参数列表中添加 - Q( 引用) :

$ ls -Q * | awk -F=:  {print "INSERT into citation_sources (source_name) VALUES (" "   " $1 "   " ");"} 
INSERT into citation_sources (source_name) VALUES ( "Black s Law Dictionary, 2nd Edition (Black 1891)" );

@shellter 指出, 这是你应该思考如何真正想要引用值的时候。

或者先通过斜线或相似的斜线管道,以获得(例如 sed "s,\\\,g" )。

光靠Awk逃跑解决它比人们想象的要狡猾得多,因为Bash吞噬了引言和逃逸。 因此,产生出它是最具有挑战性的一小段,我最后却为了它的生存而制造出一代无助的贝壳:

$ ls * | awk -F=: "{print "INSERT into citation_sources (source_name) VALUES ( " gensub(" ",""$ \\ "\ ","global",$1) " );"}"
INSERT into citation_sources (source_name) VALUES ( Black s Law Dictionary, 2nd Edition (Black 1891) );

把这个放进你的小剧本里

echo "Black s Law D icti onary"|awk  {
ESC=sprintf("%c",0x5c);
SQ=sprintf("%c",0x27);
DQ=sprintf("%c",0x22);
# then perfrom a 
gsub(SQ,ESC""SQ);
# and such. same goes for other stuff.
} 

简单到有:-)





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签