complex batch replacement linux
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/"
精彩评论