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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-11-2004, 03:40 PM
|
#1
|
|
Member
Registered: Jun 2004
Distribution: Gentoo
Posts: 44
Rep:
|
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.
|
|
|
|
07-11-2004, 03:49 PM
|
#2
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
You probably want to use sed in a script file.
|
|
|
|
07-11-2004, 04:02 PM
|
#3
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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
|
|
|
|
07-12-2004, 08:10 PM
|
#4
|
|
Member
Registered: Jun 2004
Distribution: Gentoo
Posts: 44
Original Poster
Rep:
|
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).
|
|
|
|
07-12-2004, 09:26 PM
|
#5
|
|
Member
Registered: Jul 2004
Distribution: Gentoo
Posts: 101
Rep:
|
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.
|
|
|
|
06-23-2005, 03:12 PM
|
#6
|
|
LQ Newbie
Registered: Feb 2003
Posts: 5
Rep:
|
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!
|
|
|
|
06-27-2005, 08:46 AM
|
#7
|
|
Member
Registered: Jun 2004
Distribution: Gentoo
Posts: 44
Original Poster
Rep:
|
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.
|
|
|
|
07-11-2005, 08:40 PM
|
#8
|
|
LQ Newbie
Registered: Jul 2005
Posts: 2
Rep:
|
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!
|
|
|
|
07-11-2005, 09:42 PM
|
#9
|
|
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,430
Rep:
|
Nice command line usage of perl jpgoper! Do you happen to have a perl script that does the same job?
-twantrd
|
|
|
|
05-08-2008, 02:31 AM
|
#10
|
|
LQ Newbie
Registered: May 2008
Location: Columbus
Distribution: RH, CentOS, Debian, Ubuntu, Mandrevia
Posts: 7
Rep:
|
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...
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:05 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|