LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-21-2006, 11:08 PM   #1
vivo2341
LQ Newbie
 
Registered: Oct 2004
Location: New Hampshire
Posts: 8

Rep: Reputation: 0
Change text in multiple files in multiple directories


I have a site running adsense and I have about 50 folders with subfolders in them and html files in each folder totaling about 200 folders. What I am looking to do is change some text in each file, some files may not have it but I would say 98% do. What I need to do is change word a to word b in all the files without having to go through each file individually.

Sorry if this has been asked before, I did do a search on google and here and was unable to find anything that really fit the bill.

Much thanks in advance,

Vivo
 
Old 11-22-2006, 01:22 AM   #2
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Rep: Reputation: 45
Definitely a shell script can do this. Is Word "A" pretty much fixed or have similar names in all the sub-folders??
 
Old 11-22-2006, 12:19 PM   #3
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
Let find locate all HTML files and perl do the substitution:

Code:
find top-dir -name \*.html | xargs perl -wi~ -pe 's/a/b/g'
Use the /g modifier if the word a could occur more than once on a line. Use \ba\b to allow only whole word matches.

Last edited by Quigi; 11-22-2006 at 12:23 PM.
 
Old 11-27-2006, 07:40 PM   #4
vivo2341
LQ Newbie
 
Registered: Oct 2004
Location: New Hampshire
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Micro420
Definitely a shell script can do this. Is Word "A" pretty much fixed or have similar names in all the sub-folders??
It is actually an adsense site so I need to change 1234 to 5678 in all the pages, so it is the same number through the entire site.
 
Old 11-27-2006, 07:42 PM   #5
vivo2341
LQ Newbie
 
Registered: Oct 2004
Location: New Hampshire
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Quigi
Let find locate all HTML files and perl do the substitution:

Code:
find top-dir -name \*.html | xargs perl -wi~ -pe 's/a/b/g'
Use the /g modifier if the word a could occur more than once on a line. Use \ba\b to allow only whole word matches.
so it would be 'find /home/vivo2341 -name \*.html | xargs perl -wi- -pe 's/a/1234/5678/g'

Like that? I just don't want to screw up everything and ruin something, I will do a backup and then try this tonight! Thank you both for your help.
 
Old 11-27-2006, 08:16 PM   #6
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
Quote:
Originally Posted by vivo2341
so it would be 'find /home/vivo2341 -name \*.html | xargs perl -wi- -pe 's/a/1234/5678/g'

Like that? I just don't want to screw up everything and ruin something, I will do a backup and then try this tonight! Thank you both for your help.
Close. With the italic a I was referring to your original question, asking to replace word "a". So substitute "1234" for the [i]a[/a]; the command line should be
Code:
find /home/vivo2341 -name \*.html | xargs perl -wi~ -pe 's/1234/5678/g'
You could also specify the four options separately to make clearer what's going on -- "perl -w -i~ -p -e 's/1234/5678/g'" (the single quotes are unnecessary here, but a good practice in general).

The perl option -i~ (that's i tilde) does the processing in place, and writes a backup to a file with a tilde appended. E.g., if a file foo.html is changed, you'll find the original version in foo.html~. I chose "~" to be compatible with emacs. That said, making a backup of the whole site is probably a good idea; it may be quicker to revert if something does go wrong.

To check beforehand what command line(s) will be executed, you could change "perl" to "echo perl" -- that way you'll see the (long!) command line(s) without running them.

To check afterward if it worked right, "diff foo.html{~,}" (replacing a real file name for "foo".

Be aware that, e.g., 2.5123489 will be changed to 2.5567889. If that's not desirable, tell us a bit more about which occurrences of "1234" you want to replace. Most constraints can be written as a regular expression (which we use in the s command).

Now there's more than you ever wanted to know
 
  


Reply


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy files from multiple directories into one directory MadRabbit Linux - Newbie 8 02-07-2014 07:56 PM
copying multiple different files in different directories with identical file names nickleus Linux - Software 4 03-09-2006 01:25 PM
find and copy files into multiple directories avargas22 Linux - Newbie 2 04-01-2004 11:11 AM
Help I need help tarring multiple files in multiple directories VisionZ Linux - Newbie 28 03-25-2004 05:25 PM
How to replace text in multiple files bpk Linux - Newbie 2 02-10-2004 02:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:55 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration