Solved calling sed file withing shell script

Hi,

With the help of some people on this forum, I have been able to do some basic search and replace using sed...
What I would like to do now is to combine 2 of the sed script withing a shell script.
Could someone please help?
Bellow are the 2 command that I run manually I would like to add to my script
Script one :-
find httpdocs/ -type f -exec sed -i .bak -fsedssl.txt {}\+
sedssl.txt
Code:
s|http://www.mydomain.co.uk/|/|g                       
s|="./|="/|g                                           
s|="../|="/|g                                           
s|http://ajax.googleapis.com|https://ajax.googleapis.com|g
Script two :- sedssireplace.txt
find httpdocs/ -type f -name' *.shtml' -execsed -i .bak -f sedssicon.txt {}\+
Code:
s/<!--#include virtual="/<?php include '/g
s/"-→/';?>/g

my first script is run for all file type and the scond is only on .shtml extension
Thank you
 
Hi,

With the help of some people on this forum, I have been able to do some basic search and replace using sed...
What I would like to do now is to combine 2 of the sed script withing a shell script.
Could someone please help?
Bellow are the 2 command that I run manually I would like to add to my script
Script one :-
find httpdocs/ -type f -exec sed -i .bak -fsedssl.txt {}\+
sedssl.txt
Code:
s|http://www.mydomain.co.uk/|/|g                     
s|="./|="/|g                                         
s|="../|="/|g                                         
s|http://ajax.googleapis.com|https://ajax.googleapis.com|g
Script two :- sedssireplace.txt
find httpdocs/ -type f -name' *.shtml' -execsed -i .bak -f sedssicon.txt {}\+
Code:
s/<!--#include virtual="/<?php include '/g
s/"-→/';?>/g

my first script is run for all file type and the scond is only on .shtml extension
Thank you
OK. I don't know if you've been told this by others, so I'll mention it now; meta-characters are escaped differently, depending on what your default shell is. Mine is csh(1) (tcsh), so my example(s) will be based on that. :)
Assuming that you're in the parent directory of the ones you'll be working with:
Create 2 filter files for your sed(1) regex's -- I prefer to do it this way, others may feel differently.
filter1.sed
Code:
s|http\:\/\/www\.mydomain\.co\.uk\/|\/|g
s|\=\"\.\/|\=\"\/|g
s|\=\"\.\.\/|\=\"\/|g                                          
s|http\:\/\/ajax\.googleapis\.com|https\:\/\/ajax\.googleapis\.com|g
filter2.sed
Code:
s/\<\!\-\-\#include\ virtual\=\"/\<\?php\ include\ '/g
s/\"\-\→\/\'\;\?\>/g

Now for the script to fire sed(1) against the filters (regex's).

mysedscript.sh
Code:
#!/bin/sh -

for name in $(find httpdocs/ -type f)

do
    sed -f filter1.sed <$name > temp.txt
    mv temp.txt $name
done
rm temp.txt

for name in $(find httpdocs/ -type f -name '*.shtml')

do
  sed -f filter2.sed <$name >temp.txt
  mv temp.txt $name
done
rm temp.txt

exit
This will fire find(1) against sed(1), and sed(1) against both filters. In the end, both jobs will be done.
WARNING: I made no provision for backups! I always examine, and test my regexp's before I run them against something I care about.
Just so you know. :)

--Chris
 
hi Chris_H thank you very much for you help.
This is a lot more complex that I tough.
I got most of the scirpt expect the following bit
Code:
    sed -f filter1.sed <$name > temp.txt
    mv temp.txt $name
WHat does it do?
Thank you
I also work in test dir.. so yes I get the warnings:)
fred
 
It's part of the for loop, above it. So, for every file that find(1) finds, get's passed to sed(1) with the label (macro) of $name, which sed(1) then processes with the regex filter file filter1.sed, or filter2.sed, as appropriate.
sed(1) pushes the processed file, into a temporary file named temp.txt, which is then re-named into it's original name, and the temporary temp.txt is then deleted.

Kind of hard to explain, but I hope that, at least, gets you in the right direction. :)

--Chris
 
Kind of hard to explain, but I hope that, at least, gets you in the right direction
Yes it kind of make sense now.
Thank you.
I just started my long journey in learning sed + regular expresion... not finding it easy so far:~
 
Trick #1 when you are dealing with slashes is to use a different character for the sed(1) separator. It uses the first character after the s. So rather than use a slash and have to escape every slash after that, use vertical bar like your second code section, or a percent sign, or something that will not be found in the strings:
Code:
s%a/b/c%x/y/z%
s!a/b/c!x/y/z!
s|a/b/c|x/y/z|
 
wblock@ , so you saying that I should use my original files as filter1.sed and filter2.sed
Code:
s|http://www.mydomain.co.uk/|/|g                      
s|="./|="/|g                                          
s|="../|="/|g                                          
s|http://ajax.googleapis.com|https://ajax.googleapis.com|g
Code:
s/<!--#include virtual="/<?php include '/g
s/"-→/';?>/g
and use Chris_H shell script to call them?
is that right?
 
Back
Top