[PLUG] replacing multipule instances of text

Paul Heinlein heinlein at madboa.com
Tue Sep 30 08:02:02 UTC 2003


On Tue, 30 Sep 2003, alex wrote:

> I need to replace all the homepage links in the HTML with the
> updated address.
>
> ex. replace www.foobar.com/RCTC/ with www.RCTC.com/
>
> My question is..... is there an easy way to do this other than the
> obvious highlight and pasting of text?

Here's what I'd do, assuming that your example is correct. Let's say
that the documents are all in /var/www/rctc/public_html.

  #!/bin/sh
  #
  # out with old, in with the...
  oldurl="www.foobar.com/RCTC/"
  newurl="www.RCTC.com/"
  # make a backup copy of the document root
  mkdir /tmp/rctc
  cd /var/www/rctc
  tar cf - ./public_html | ( cd /tmp/rctc && tar xf -)
  # using the backup as a base, overwrite the production copies
  cd /tmp/rctc
  for f in $(grep -lr "$oldurl" public_html); do
    sed -e "s!$oldurl!$newurl!g" < $f > /var/www/rctc/$f
  done
  # make sure any remaining foobar references are valid
  grep -r 'www\.foobar\.com' /var/www/rctc/public_html/*

If something goes horribly wrong, you can always restore the temp copy
of the tree to the production environment. Otherwise, delete the temp
tree and head off to enjoy a cold beverage of your choice. :-)

--Paul Heinlein <heinlein at madboa.com>




More information about the PLUG mailing list