LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Find and replace text in multiple file (http://www.linuxquestions.org/questions/linux-software-2/find-and-replace-text-in-multiple-file-203801/)

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:

EXAMPLE:
Code:

TMPFILE=/tmp/tmp.$$

for f in *.txt; do
  sed 's/OLD_STRING/NEW_STRING' $f > $TMPFILE
  exit 1    # DEBUG
  mv $TMPFILE $f
fi

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/tmp.xxx" 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,....:

Code:

<!-- BEGIN trafic.ro code v2.0 -->
<script>t_rid="xxxxxx";</script>
<script src="http://storage.trafic.ro/js/trafic.js"></script>
<noscript><a href="http://www.trafic.ro/top/?rid=xxxxxxx">
<img src="http://log.trafic.ro/cgi-bin/pl.dll?rid=xxxxxxxxx"
 border=0 alt="trafic ranking"></a></noscript>
<!-- END trafic.ro code v2.0 -->

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

Code:

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


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):

Quote:

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

oldexpression:
<script type ="text/javascript" src='http://www.business-plan-development.com/astracker/ast.php'></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?

-twantrd

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
:%s/oldtextstring/newtextstring/g

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 04:51 PM.