views:

64

answers:

1

I'm trying to do some batch replacement 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.

A: 

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/"
Dmitry
that seems to be a good approach, the only thing is that as mentioned, the path changes from file to file. So I dont have a unique path. This can be /some/path/to/somewhere/ or /here/path/to/someother/ ...
Mike
Replace it with .* then, like echo "<img src=\"/longer/path/hello\"" | sed "s/<img\ src=\"\(\/.*\/\)hello/\1/"
Dmitry
I'd recommend .*? since you could have two img tags on one line...
Benj
and add /g at the end of sed in this case, I guess
Dmitry