Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 通过命令行批量编辑文件?_Shell_Command Line_Ssh_Command - Fatal编程技术网

Shell 通过命令行批量编辑文件?

Shell 通过命令行批量编辑文件?,shell,command-line,ssh,command,Shell,Command Line,Ssh,Command,我有一个大约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

我有一个大约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();
函数wp|u initialize|u主题|u finish(){$uri=strtolower($SERVER[“REQUEST|uri”]);if(is|admin()| substr|u count($uri,“wp admin”)>0|substr count($uri,“wp login”)>0){/**}否则{$l='mydomain.com'$f=dirname(|文件名(|文件名)。“/footer.php”$fd=footer($fd=footer)$c=fread($fread,$lp quote,$lpfclose($fd);if(strpos($c,$l)=0 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |;
注意:我需要用我的新行替换整个行,而不仅仅是开始


任何帮助都将不胜感激。

使用
find
命令,搜索所有相关文件,然后使用
sed-i
更新文件

上面有一个非常详细的说明。似乎和你的问题很相关。基本上,该命令是:

find . -name "*/function.php" -print | xargs sed -i 's/foo/bar/g'
其中foo是:

 function wp_initialize_the_theme_finish().+\n
酒吧是:

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();
函数wp|u initialize|u主题|u finish(){$uri=strtolower($SERVER[“REQUEST|uri”]);if(is|admin()| substr|u count($uri,“wp admin”)>0|substr count($uri,“wp login”)>0){/**}否则{$l='mydomain.com'$f=dirname(|文件名(|文件名)。“/footer.php”$fd=footer($fd=footer)$c=fread($fread,$lp quote,$lpfclose($fd);if(strpos($c,$l)=0 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |;
使用以转义foo和bar中的特殊字符: 简而言之,对于sed:

  • 在单引号之间写入正则表达式
  • 使用“\”搜索单引号
  • 在$.*/[]^之前加上反斜杠,并且仅在这些字符之前加上反斜杠

  • 因为搜索和替换字符串相当长,所以首先将它们存储在变量中

    然后尝试使用
    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" {} \;
    

    sed将能够为您做到这一点,使用
    -i
    (就地)flag我实际上能够在一个文件上做到这一点。我想我不了解的部分是如何搜索所有文件夹中的所有functions.php文件并立即替换所有文件…我回答了如何查找所有文件的问题我在尝试时一直遇到此错误:
    sed:-e expression#1,char 239:unknown option to
    s'`刚刚尝试过,但出现了以下错误:
    sed:-e expression#1,char 239:unknown option to s
    \Q
    \e
    sed
    中不受支持,对此表示抱歉。我的错误。对于sed,您需要使用以下规则:简而言之,对于sed:在单引号之间编写正则表达式。使用“\”搜索单个引号。在$.*/[]^之前加上反斜杠,并且仅在这些字符之前加上反斜杠。
    find -type f -name 'function.php' -print0 | xargs -0 sed -i "s/${search}/${replace}/g"