-   Linux - Software (
-   -   Find and replace text in multiple file (

Bad_Bob 07-11-2004 03:40 PM

Find and replace text in multiple file
I have about 500 html files in whic I need to change the words at the bottom (they must be hard coded because I have to put them on a cd)

Is there any appication that will do multiple find and replace? I tried Kate, but it only searches the current file.

jschiwal 07-11-2004 03:49 PM

You probably want to use sed in a script file.

paulsm4 07-11-2004 04:02 PM

Yup - "sed" is probably the way to go:



for f in *.txt; do
  exit 1    # DEBUG
  mv $TMPFILE $f

Notice that line "exit 1"? The example script is *destructive* (it will overwrite your current files).

So, to be safe, I'd:
1. Make a "tar" backup copy of my files before I edited them

2. Run the script with the "exit 1", and make sure the output file "/tmp/" was what I expected

3. Then, and only then, comment out the "exit 1" (change it to "# exit 1") and actually modify the files.

Hope that helps .. PSM

Bad_Bob 07-12-2004 08:10 PM

This script is working, thanks!

One more question, how can I replace strings that include an apostrophe ( ' )
That seems to be what teminates the string. (I tried %27, but that didn't work).

p-static 07-12-2004 09:26 PM

Try \027. maybe. But i'm too lazy to actually check if it works.

paulsm, I thought that for loops in bash ended with done, not fi.

blaci 06-23-2005 03:12 PM

I want to replace a tracking code from an old one to a new one:

The old tracking code code contains newlines,....:


<!-- BEGIN code v2.0 -->
<script src=""></script>
<noscript><a href="">
<img src=""
 border=0 alt="trafic ranking"></a></noscript>
<!-- END code v2.0 -->

I want to replace this code from all files. I tryed this, but doesn't make it.


for f in *
  sed -i 's/<!-- BEGIN code v2.0 -->\*<!-- END code v2.0 -->/NEWSCRIPT/g' $f
  echo "$f";

How to make it work?

Thanks for your answer!

Bad_Bob 06-27-2005 08:46 AM

I'm about finished with the project I started this post with. sed is really cool, but I found it was not powerfull enought for my needs, so I wrote the script in php.

jpgoper 07-11-2005 08:40 PM

I found this to make a find&replace in the whole directory's files (inc subdirs):


find . -name '*.html' | xargs perl -pi -e 's/oldtext/newtext/g'
And it is good but it fails when using expressions like this:

<script type ="text/javascript" src=''></script>

new expression:
<? include($ruta."/include/clicks.php");?>

I try using \ before spaces,>,<,",',/,$,(,),?,; but it gives me a prompt, as if it was waiting for more parameters.

Can anyone help me please!

twantrd 07-11-2005 09:42 PM

Nice command line usage of perl jpgoper! Do you happen to have a perl script that does the same job?


mccwho 05-08-2008 02:31 AM

Vi is another option, but mostly for a single file at a time.
IF you wanted to replace some text in a single file you can use vi.
while in vi press

The "g" will replace all instances of the old text with the new string. This has been a huge time save in those huge vhosts or httpd config files.
ie..such as changing the IP of a server, etc...

All times are GMT -5. The time now is 07:30 AM.