我使用这段代码搜索和替换当前目录中所有php文件中的文本:
for file in `find * -type f -name "*.php"`; do awk 'BEGIN { RS="" } FILENAME==ARGV[1] { s="$0" } FILENAME==ARGV[2] { r="$0" } FILENAME==ARGV[3] { gsub(s,r) ; print }' "zsearch" "zreplace" "$file" > "$file"tmp7 && mv "$file"tmp7 "$file";echo "$file"; done如果zsearch文件的内容:
<br>
<center><!--LiveInternet counter--><script type="text/javascript"><!--它运行得很好,但是如果zsearch文件的内容:
<!--LiveInternet counter--><script type="text/javascript"><!--
document.write("<a href='http://www.liveinternet.ru/click' "+
"target=_blank><img src='http://counter.yadro.ru/hit?t16.6;r"+
escape(document.referrer)+((typeof(screen)=="undefined")?"":
";s"+screen.width+"*"+screen.height+"*"+(screen.colorDepth?
screen.colorDepth:screen.pixelDepth))+";u"+escape(document.URL)+
";"+Math.random()+
"' alt='' title='LiveInternet: показано число просмотров за 24"+
" часа, посетителей за 24 часа и за сегодня' "+
"border=0 width=88 height=31></a>")//--></script><!--/LiveInternet-->它不起作用了,不能取代
我想是因为在document.write("中插入了document.write("符号
也许我需要摆脱像+ or *这样的符号
发布于 2017-08-31 23:36:54
您的主要问题是使用regexp而不是字符串,尝试这样做(也改进了您的文件循环):
find * -type f -name "*.php" |
while IFS= read -r file; do
    awk '
        BEGIN { RS="" }
        FILENAME==ARGV[1] { old=$0 }
        FILENAME==ARGV[2] { new=$0 }
        FILENAME==ARGV[3] {
            if ( start = index($0,old) ) {
                $0 = substr($0,1,start-1) new substr($0,start+length(old))
            }
            print
        }
    ' "zsearch" "zreplace" "$file" > "$file"tmp7 &&
    mv "$file"tmp7 "$file"
    echo "$file"
done对于名字中有新行的文件,它仍然会失败--如果这是可能的话,请告诉我们。
https://stackoverflow.com/questions/45991232
复制相似问题