Solved find and replace script

Hello,

I am trying to perform a search and replace but so far, I cannot get grep to retun any results.
I am trying to write a very simple script that will search all the files with extension .shtml for a givendirectory and replace
Code:
<!--#include virtual="
with
Code:
<?php include '
and
Code:
"-->
with
Code:
';?>

Can anyone help please
Thank you
 
Something like:

find /path -type f -name '*.shtml' -exec sed -i .bak -e 's/<!--#include virtual="/<?php include \'/g' -e 's/"-->/\';?>/g' {} \+

That would do the replacement "in place" because of the -i option and a backup of the original would be left with a .bak extension. The expressions may look cryptic but it's the basic s/pattern/replacement/flags of sed(1), just locate the / signs and it should be clear. There are single quotes in the text you're matching and replacing, those have to be escaped like \' to avoid them being interpreted as the closing single quotes for the -e 'expression' options.

Test first on a backup copy of the files.
 
Thank you for the explaintion that you gave me with the scrip kpa
When I run the command, I get
find httpdocs/ -type f -name '*.shtml' -exec sed -i .bak -e 's/<!--#include virtual="/<?php include \'/g' -e 's/"-->/\';?>/g' {} \+
Code:
Unmatched ".
Does that mean it could find any files with extention .shtml or nothing to replace?
 
I forgot one character that needs to be escaped, the " character:

Code:
-e 's/<!--#include virtual=\"/<?php include \'/g'
 
Well scratch that, the patterns have so many special characters that it's better to put the expressions into a command file, let's say sedcmd.txt:

Code:
s/<!--#include virtual="/<?php include '/g
s/"-->/';?>/g

Then run the command like like this:

find httpdocs/ -type f -name '*.shtml' -exec sed -i .bak -f sedcmd.txt {} \+

This avoids all the gimmicks with shell special characters that are very difficult to escape properly. Note how none of the characters in sedcmd.txt needed any escaping because sed(1) reads it in directly without the shell doing its own parsing.
 
kpa thank you for your help:)
I actully find it easier to understand it when you moved the code to a file.
I think i'll be able to do more sed in the future based on that example..

Thank you very much.. all good now
 
kpa since my last entry, I have used and successfully adapted your script for my purpurse but I cannot figure this one out...
I am tryin to replace http://www.mydomain.co.uk/images with /images
In my sed file I tried
Code:
s/http://www.mydomain.co.uk/images/ /g
but I get
find httpdocs/ -type f -name '*.shtml' -exec sed -i .bak -f sedhttpscmd.txt {} \+
Code:
sed: ww.mydomain.co.uk/images/ /g: No such file or directory
Also tried
Code:
s/http://www.mydomain.co.uk/images//\images/g
 
Back
Top