LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-08-2010, 11:04 AM   #1
bckr
LQ Newbie
 
Registered: Jul 2010
Posts: 6

Rep: Reputation: 0
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!
 
Old 07-08-2010, 11:15 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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

Last edited by druuna; 07-08-2010 at 11:18 AM. Reason: Added .bak to -i switch for backups.
 
1 members found this post helpful.
Old 07-08-2010, 03:19 PM   #3
bckr
LQ Newbie
 
Registered: Jul 2010
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 07-08-2010, 08:36 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Add the letter 'g' at the end of your sed, ie after last % but before '
 
1 members found this post helpful.
Old 07-09-2010, 01:59 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

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

find . -type f -iname "*.html" -exec sed -i.bak 's%/awstats/%awstats/%g' {} \;
 
Old 07-09-2010, 02:16 AM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
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.
 
Old 07-09-2010, 02:19 PM   #7
bckr
LQ Newbie
 
Registered: Jul 2010
Posts: 6

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


Reply

Tags
find, sed



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
How do I Find and Replace text within files using Perl (no one-liners please) GuerillaSquad Programming 3 12-31-2009 09:01 AM
Can I use GREP to find & replace text? jim.thornton Linux - Newbie 3 07-18-2008 06:36 PM
Find and replace text in multiple file Bad_Bob Linux - Software 9 05-08-2008 02:31 AM
Find text and replace another field jaysin_aus Linux - Server 9 07-26-2007 02:16 PM
How to 'Find and Replace' text in vi editor? concoran Programming 2 11-21-2001 09:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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