我有一个大约500个文件夹的列表。每个文件夹中都有一个functions.php
文件。
我需要在每个functions.php
文件中搜索以下文字:
function wp_initialize_the_theme_finish()
我需要用以下内容替换任何具有上述文本的行:
function wp_initialize_the_theme_finish() { $uri = strtolower($_SERVER["REQUEST_URI"]); if(is_admin() || substr_count($uri, "wp-admin") > 0 || substr_count($uri, "wp-login") > 0 ) { /* */ } else { $l = 'mydomain.com'; $f = dirname(__file__) . "/footer.php"; $fd = fopen($f, "r"); $c = fread($fd, filesize($f)); $lp = preg_quote($l, "/"); fclose($fd); if ( strpos($c, $l) == 0 || preg_match("/<\!--(.*" . $lp . ".*)-->/si", $c) || preg_match("/<\?php([^\?]+[^>]+" . $lp . ".*)\?>/si", $c) ) { wp_initialize_the_theme_message(); die; } } } wp_initialize_the_theme_finish();
注意:我需要用新行替换整行,而不仅仅是开头。
非常感谢任何帮助。
答案 0 :(得分:0)
使用find
命令搜索所有相关文件,然后使用sed -i
更新文件
答案 1 :(得分:0)
上面写着非常详细的article。似乎很好地与您的问题相关。基本上命令是:
find . -name "*/function.php" -print | xargs sed -i 's/foo/bar/g'
foo在哪里:
function wp_initialize_the_theme_finish().+\n
和bar是:
function wp_initialize_the_theme_finish() { $uri = strtolower($_SERVER["REQUEST_URI"]); if(is_admin() || substr_count($uri, "wp-admin") > 0 || substr_count($uri, "wp-login") > 0 ) { /* */ } else { $l = 'mydomain.com'; $f = dirname(__file__) . "/footer.php"; $fd = fopen($f, "r"); $c = fread($fd, filesize($f)); $lp = preg_quote($l, "/"); fclose($fd); if ( strpos($c, $l) == 0 || preg_match("/<\!--(.*" . $lp . ".*)-->/si", $c) || preg_match("/<\?php([^\?]+[^>]+" . $lp . ".*)\?>/si", $c) ) { wp_initialize_the_theme_message(); die; } } } wp_initialize_the_theme_finish();
使用following rules转义foo和bar中的特殊字符: 简而言之,对于sed:
答案 2 :(得分:0)
由于搜索和替换字符串相当长,因此首先将它们存储在变量中。
然后使用find
选项
sed
和-exec
#!/bin/bash
search='^.*function wp_initialize_the_theme_finish().*$'
replace='function wp_initialize_the_theme_finish() { $uri = strtolower($_SERVER["REQUEST_URI"]); if(is_admin() || substr_count($uri, "wp-admin") > 0 || substr_count($uri, "wp-login") > 0 ) { /* */ } else { $l = "mydomain.com"; $f = dirname(__file__) . "/footer.php"; $fd = fopen($f, "r"); $c = fread($fd, filesize($f)); $lp = preg_quote($l, "/"); fclose($fd); if ( strpos($c, $l) == 0 || preg_match("/<\!--(.*" . $lp . ".*)-->/si", $c) || preg_match("/<\?php([^\?]+[^>]+" . $lp . ".*)\?>/si", $c) ) { wp_initialize_the_theme_message(); die; } } } wp_initialize_the_theme_finish();'
find -type f -name 'function.php' -exec sed -i "s/${search}/${replace}/g" {} \;
使用xargs
find -type f -name 'function.php' -print0 | xargs -0 sed -i "s/${search}/${replace}/g"