Click to See Complete Forum and Search --> : BASH Search & Replace with Sed


OhLordy
07-30-2003, 05:22 AM
Hi I have writen this little script to replace

document.write(id);
with
document.write(id);
document.write('&group_id=');
document.write(group_id);

this is what I came up with


for file in `find . -name '*.tpl'`
do
mv $file buffer
sed 's/document.write\(id\)\;/document.write\(id\)\;\ndocument.write\(\'&group_id=\'\)\;\ndocument.write\(groupd_id\)\;\n/g' buffer > $file
rm buffer
done

I've tried all number of different slashes but I keey getting errors. The one that I can't seem to get rid of now is
./replace: line 4: unexpected EOF while looking for matching `''
./replace: line 7: syntax error: unexpected end of file
but I can't really figure out what it's trying to say.

Thanks in advance for any help
Rob

dchidelf
07-30-2003, 12:57 PM
You aren't really replacing anything, you are just appending 2 additional lines after each
document.write(id);

I think this works for what you need:

sed "/document.write(id);/a\\
document.write('&group_id=');\\
document.write(groupd_id);"


Because your text includes a ' it would be easier to use " to quote your command.