Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 07-11-2004, 04:40 PM   #1
Registered: Jun 2004
Distribution: Gentoo
Posts: 44

Rep: Reputation: 15
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.
Old 07-11-2004, 04:49 PM   #2
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 658Reputation: 658Reputation: 658Reputation: 658Reputation: 658Reputation: 658
You probably want to use sed in a script file.
Old 07-11-2004, 05:02 PM   #3
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
Old 07-12-2004, 09:10 PM   #4
Registered: Jun 2004
Distribution: Gentoo
Posts: 44

Original Poster
Rep: Reputation: 15
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).
Old 07-12-2004, 10:26 PM   #5
Registered: Jul 2004
Distribution: Gentoo
Posts: 101

Rep: Reputation: 15
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.
Old 06-23-2005, 04:12 PM   #6
LQ Newbie
Registered: Feb 2003
Posts: 5

Rep: Reputation: 0
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!
Old 06-27-2005, 09:46 AM   #7
Registered: Jun 2004
Distribution: Gentoo
Posts: 44

Original Poster
Rep: Reputation: 15
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.
Old 07-11-2005, 09:40 PM   #8
LQ Newbie
Registered: Jul 2005
Posts: 2

Rep: Reputation: 0
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!
Old 07-11-2005, 10:42 PM   #9
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

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

Old 05-08-2008, 03:31 AM   #10
LQ Newbie
Registered: May 2008
Location: Columbus
Distribution: RH, CentOS, Debian, Ubuntu, Mandrevia
Posts: 7

Rep: Reputation: 0
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...


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 07:02 AM
Replace text of unknown content with other text in file brian0918 Programming 15 07-14-2005 10:22 PM
Replace text of unknown content with other text in file brian0918 Linux - Software 1 07-14-2005 04:22 PM
How to replace text in multiple files bpk Linux - Newbie 2 02-10-2004 03:03 PM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 02:56 PM

All times are GMT -5. The time now is 03:47 AM.

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