开发者

complex batch replacement linux

开发者 https://www.devze.com 2022-12-11 12:43 出处:网络
I\'m trying to do some batch repla开发者_JS百科cement for/with a fairly complex pattern So far I find the pattern as:

I'm trying to do some batch repla开发者_JS百科cement for/with a fairly complex pattern

So far I find the pattern as:

find '(' -name '*.php' -o -name '*.html' ')' -exec grep -i -n 'hello' {} +

The string I want to replace is currently as follow:

<img src="/some/path/to/somewhere/hello" /> 

where the path for the image varies but always contain the sub-string 'hello' at the end

I would like to grab the path and perform a replacement as follow:

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

What would a good way to perform this?

Any help will be appreciate it.


Replace -exec grep ..... with

-exec cat '{}' | sed s/<img src="(/some/path/to/somewhere/)hello" />/<img src="<?php myfunction('(\1)(constant)'); ?>" />/ > /tmp/output; mv /tmp/output '{}' ';'

escaping spaces, " and / symbols in sed's search and replacement patterns with backslahes as sed likes.

For instance, this will extract /path/:

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

精彩评论

暂无评论...
验证码 换一张
取 消