LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   find and replace text from many files in a dir (http://www.linuxquestions.org/questions/linux-newbie-8/find-and-replace-text-from-many-files-in-a-dir-818717/)

bckr 07-08-2010 11:04 AM

find and replace text from many files in a dir
 
Hello everyone,

I have a many directories each with about 20 html files inside. All the files have .html ext. What I'm hoping is possible is from command line to find some text in each one and replace it with some other text.

Basically what I want to replace is;

/awstats/
with
awstats/

I can do this easily with dreamweaver or some other application but because I have 960 pages total to do I'm hoping to do it this way.

Thanks for any help you can provide!

druuna 07-08-2010 11:15 AM

Hi,

You can combine find and sed.

Something like this should work:

find . -type f -iname "*.html" -exec sed -i.bak 's%/awstats/%awstats/%' {} \;

This finds all html files from the dir you execute the command (and all subdirectories) and changes /awstat/ into awstat/. This is done in-place (the -i switch).

Do test this first!! (create a temp directory, copy a few html files into it and execute this command from within that directory). I did test it on 2 files and that worked, but you need to be sure.

Hope this helps.

EDIT
I just added the .bak part to the -i switch, this will create a backup of the original.
/EDIT

bckr 07-08-2010 03:19 PM

druuna,

Thank you so much! This does work. I used it in test dir on one of the files I need to work with. However, there is only 1 issue.

It only changes the first instance it finds in any block of html. I spent some time trying to figure out why and as a test just ran the script again and it got the next instance, a third got the third.

So if it's just a matter of running it 5-6 times in each directory I can do that but would like to know if there is a way around doing so.

Also, one other thing I should have asked when i first posted was how would I change the script to be run from outside of the directories so I can do them all at once instead of going into each directory and running the script? I have something like 60 total directories that need to be done.

Thank you for your help so far.

grail 07-08-2010 08:36 PM

Add the letter 'g' at the end of your sed, ie after last % but before '

druuna 07-09-2010 01:59 AM

Hi,

grail already gave the correct answer, just to make sure:

find . -type f -iname "*.html" -exec sed -i.bak 's%/awstats/%awstats/%g' {} \;

AnanthaP 07-09-2010 02:16 AM

If the directories are all in one place (ie all are sub directories, sub-sub directories of a parent folder say "~/src"), then you could simply change the . after find to ~/src since find traverses sub directories by default.

bckr 07-09-2010 02:19 PM

Yes, thank you grail and all others who replied. I have used this information and have things working just the way I needed. Very helpful.

Thank you to you all.


All times are GMT -5. The time now is 01:53 AM.