Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/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
Regex 复杂批量替换linux_Regex_Batch File_Replace - Fatal编程技术网

Regex 复杂批量替换linux

Regex 复杂批量替换linux,regex,batch-file,replace,Regex,Batch File,Replace,我正在尝试用一个相当复杂的模式进行批量替换 到目前为止,我发现模式如下: find '(' -name '*.php' -o -name '*.html' ')' -exec grep -i -n 'hello' {} + 我要替换的字符串当前如下所示: <img src="/some/path/to/somewhere/hello" /> <img src="<?php myfunction('(/some/path/to/somewhere/)'); ?>

我正在尝试用一个相当复杂的模式进行批量替换

到目前为止,我发现模式如下:

find '(' -name '*.php' -o -name '*.html' ')' -exec grep -i -n 'hello' {} +
我要替换的字符串当前如下所示:

<img src="/some/path/to/somewhere/hello" /> 
<img src="<?php myfunction('(/some/path/to/somewhere/)'); ?>" /> 

其中图像的路径不同,但始终在末尾包含子字符串“hello”

我想获取路径并执行如下替换:

<img src="/some/path/to/somewhere/hello" /> 
<img src="<?php myfunction('(/some/path/to/somewhere/)'); ?>" /> 
“/>
执行此操作的好方法是什么


任何帮助都将不胜感激。

-exec grep…
替换为

-exec cat '{}' | sed s/<img src="(/some/path/to/somewhere/)hello" />/<img src="<?php myfunction('(\1)(constant)'); ?>" />/ > /tmp/output; mv /tmp/output '{}' ';'
-exec cat'{}'| sed s/“/>/>/tmp/output;mv/tmp/输出'{}';'
sed搜索和替换模式中的“转义空格”和/或符号,如sed所愿,使用后斜杠

例如,这将提取/path/:

echo "<img src=\"/path/hello\"" | sed "s/<img\ src=\"\(\/path\/\)hello/\1/"

echo”这似乎是一个很好的方法,唯一的一点是,正如前面提到的,路径会随着文件的变化而变化。所以我没有一条独特的道路。这可以是/some/path/to/somewhere/或/here/path/to/someother/…将其替换为.*然后,像echo一样“并在本例中的sed末尾添加/g