1748088 Members
4857 Online
108758 Solutions
New Discussion юеВ

script - help

 
SOLVED
Go to solution
Pat Peter
Occasional Advisor

script - help

Hi,

I have a nearly 120 html files in one server. Now we have migrated the files to a new server.

So I have written a redirect script which would redirect to the new server.

Now I would need to remove the contents of each html file and put the content of my redirect script in each of the HTML file.

Can anyone please tell me if I can remove the entire content of each file and put the content of my redirect script in each file.

I am not good in PERL or SHELL scripting.

Please help.

Thanks,
Pat
12 REPLIES 12
Geoff Wild
Honored Contributor

Re: script - help

Use sed

sed 's/serverold/servernew/g' somefile.html > somenewfile.html

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Rodney Hills
Honored Contributor

Re: script - help

If you're talking about doing a massive update to several html files where you need to change the paths to the new server, then perl could do it in one line-

perl -i -p -e 's{old/path}{new/path}g' *.html

HTH

-- Rod Hills
There be dragons...
Pat Peter
Occasional Advisor

Re: script - help

HI,

I think I was not very clear on my requirement in my thread.

What I want to do is

1. remove the entire contents of existing .html files in my directory.
2. Copy the content of one of the files in another directory to all the .html files in my directory.

Hope this one is littl emore clear.

Please help me.

Thanks,
Pat
Rick Garland
Honored Contributor

Re: script - help

To remove the contents of the existing html files in your directory

ls *.html | while read line
do
cat /dev/null > $line
done
This will keep the files in place but they will have 0 bytes in size.

To put something in a file
ls *.html | while read line
do
echo "This is the new stuff" > $line
done
James R. Ferguson
Acclaimed Contributor

Re: script - help

Hi Pat:

Based upon your last specification you could do this to overlay all of your files with a copy of another:

# find -type f -name "*.html" | xargs -i cp /tmp/overlay {}

The /tmp/overlay file will replace every file in whose filename ends in ".html".

Regards!

...JRF...
Geoff Wild
Honored Contributor
Solution

Re: script - help

Clear as mud - sorry...

Are you saying, you want to keep all 120 html files, but make them empty? and then copy the contents of 1 file to all 120 html files?

cd /directoryof/existing/files

for i in `ls`
do
> $i
done

for i in `ls`
do
cat /directoryof/myredirectscript > $i
done

Rgds...Geoff

Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Geoff Wild
Honored Contributor

Re: script - help

Sorry - for html only, change my ls command to `ls *.html` or `ls *.htm` just incase there are other files you don't want clobbered...

While we are at it - make sure you have a good backup of those files...

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Pat Peter
Occasional Advisor

Re: script - help

Hi Rick,

The first part in your solution really helps. I am able to create the empty files by doing that.

But in the second step, I have a file called "redirect". I need to copy the content of this file to all the .html files.

Thanks,
Pat
James R. Ferguson
Acclaimed Contributor

Re: script - help

Hi (again) Pat:

Unless I am misunderstanding you, you need to reexamine tha suggestion I offered using 'find' and 'xargs'. See the manpages for 'xargs' (particulary) to see how this technique works.

find -type f -name "*.html" | xargs -i cp redirect {}

Regards!

...JRF...