LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-20-2003, 08:31 PM   #1
xtrude
LQ Newbie
 
Registered: Jan 2003
Posts: 1

Rep: Reputation: 0
Question Search and replace links


Hello all,
I am trying to figure out a way to search and replace text links within pages in all web folders on our server at once.

Ex: replace all instances of www.wherever.com
with : www.here.com

Any ideas? I am not local to the machine, so all work is done through remote ssh1 connect in shell.
I tride using this:

perl -pi -e "s/www\.oldsite\.com/www\.newsite\.com/g;" *.html

But, perl doesn't seem to like going recursively into directories.
Any help would be appreciated. Thanks,

Xtrude
 
Old 01-29-2003, 10:02 AM   #2
vladkrack
Member
 
Registered: Oct 2002
Location: Curitiba - Brazil
Distribution: Conectiva
Posts: 334

Rep: Reputation: 30
Try with sed:

sed 's/www.wherever.com/www.here.com/g' *.html

Hope that helps ...
 
Old 01-29-2003, 10:08 AM   #3
MasterC
LQ Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613

Rep: Reputation: 69
vlad, would that work on subdirectories as well? And, would it work for things like email adresses (in the html form <a href="mailto"whatever@whatever.com">Webmaster</a> and changing just the "whatever@whatever.com" to "Whomever@whatever.com")?



Thanks
 
Old 01-29-2003, 10:25 AM   #4
vladkrack
Member
 
Registered: Oct 2002
Location: Curitiba - Brazil
Distribution: Conectiva
Posts: 334

Rep: Reputation: 30
I forgot something, using sed will show the result as stdout, so it's necessary to direct it to a file:

sed 's/expression/other expression/g' file > other file

So It should be easier with a shell ( could be used with email addresses too, and subdirectories ) :

# for file in `find . -iname *.html`; sed 's/whatever@whatever.com/whontever@whatever.com/g' $file > $file.new ; done
 
Old 01-29-2003, 11:55 AM   #5
0x0001
Member
 
Registered: Sep 2002
Distribution: Slackware 8.1
Posts: 84

Rep: Reputation: 15
I did something like this once, when our webserver (at the university was reconfigured):
It's a bit more complicated than the above versions since i use vim
to do the editing.

basically the server was moved so it was accessed
through student.cosy.sbg.ac.at
instead of ww.cosy.sbg.ac.at
the following did the trick, the www2stud file is attacked.
Grep only reduces the number of replacements done, but it can be
removed without making much of a difference.

#!/bin/bash
script=/home/stud1/user/bin/www2stud
repl=0

for item in `find -name '*.html'`
do
echo -n "scanning ${item} ... "
repl=`egrep -c 'http://www.cosy.sbg.ac.at/~user' ${item}`
if [ $repl -ge 1 ]
then
echo "replacing"
`vim -e -s ${item} < ${script}`
else
echo "clean"
fi
done

www2stud:
------------
:%s/\(http:\/\/\)www\(\.cosy\.sbg\.ac\.at\/\~user\)/\1student\2/
:x
-----------
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python search and replace Accordion Programming 1 02-22-2005 07:54 PM
Batch search and replace devilkin Linux - Newbie 2 02-14-2005 02:39 AM
Emacs search and replace neil Linux - Software 1 01-28-2005 06:04 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
Grep for search, but what for replace? TheSpecial Linux - Software 18 04-28-2003 09:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration