LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 07-11-2004, 03:40 PM   #1
Bad_Bob
Member
 
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, 03:49 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You probably want to use sed in a script file.
 
Old 07-11-2004, 04:02 PM   #3
paulsm4
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:

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
 
Old 07-12-2004, 08:10 PM   #4
Bad_Bob
Member
 
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, 09:26 PM   #5
p-static
Member
 
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, 03:12 PM   #6
blaci
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,....:

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!
 
Old 06-27-2005, 08:46 AM   #7
Bad_Bob
Member
 
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, 08:40 PM   #8
jpgoper
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):

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!
 
Old 07-11-2005, 09:42 PM   #9
twantrd
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?

-twantrd
 
Old 05-08-2008, 02:31 AM   #10
mccwho
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
:%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...
 
  


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:34 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