2012-07-10 15 views
0

find/cygdrive/c/xampp/htdocs/news4u -type f -exec sed -i's/document.write(unescape(「%3Cscript src ='」+ gaJsHost +「google-analytics.com/ga.js 'type ='text/javascript'%3E%3C/script%3E「)); /(function(){var ga = document.createElement('script'); ga.type ='text/javascript'; ga。 async = true; ga.src =('https:'== document.location.protocol?'https:// ssl':'http:// www')+'.google-analytics.com/ga.js' ; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga,s);})();/g'{} \;如何逃避下面的序列,以便在shell腳本中使用sed替換文本?

這導致的誤差sed: -e expression #1, char 93: unknown option to s'`

我使用Windows和使用 '的cygwin' 運行shell腳本。

如何解決這個問題,並應該單引號被轉義,因爲它再次被包含在單引號?

回答

0

逃逸/是在sed正則表達式(例如這裏:google-analytics.com/ga.js),或使用其它的分隔符(例如的代替s^^^s///)。

+0

我用'find/cygdrive/c/xampp/htdocs/news4u/apps/frontend/modules/alerts/templates -type f -exec sed -i'/ document.write(unescape(「%3Cscript src =' 「+ gaJsHost +」google-analytics.com \ /ga.js'type ='text \/javascript'%3E%3C \/script%3E「)); /(function(){var ga = document.createElement( 'script'); ga.type ='text \/javascript'; ga.async = true; ga.src =('https:'== document.location.protocol?'https:\/\/ssl':' http:\/\/www')+'.google-analytics.com \ /ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga,s); })();/G {} \; ' – 2012-07-10 10:22:12

+0

我逃脫了'/'而且它不起作用。還有什麼可以改變的? – 2012-07-10 10:23:17

+0

你不僅必須''逃脫''而且'''也是。最好不要轉義'/',而是使用其他字符作爲's'的分隔符。 – 2012-07-10 10:59:03

0

要逃脫/您可以使用另一個分隔符,如Igor所述。

echo //// | sed '[email protected]////@[email protected]' 

但是有很多'也需要進行轉義。

至少有兩種方式:

第一招:

echo "111'222\"333" | sed 's/1'\''2/___/;s/2"3/__/;' 

第二個:

echo "111'222" | sed "s/1'2/___/;s/2\"3/__/;" 

你不能只是逃避''\'',因爲它絕對不在'之間。逃生角色在那裏不起作用。所以你首先需要讓轉義字符從'轉義出來,然後才能使用它。因此我們使用''\'''構造。 但是轉義字符能正常工作,因此您可以使用"\""

find /cygdrive/c/xampp/htdocs/news4u/apps/frontend/modules/alerts/templates \ 
-type f -exec sed -i "[email protected](unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));@(function() { var ga = document.createElement('script'); ga.type = 'text\/javascript'; ga.async = true;ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com\/ga.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);})();@g" {} \; 

應該可以正常工作。