Solved

UNIX Command Needed: How do I perform search/replace word on all html pages in subfolders

Posted on 2011-02-16
3
375 Views
Last Modified: 2012-05-11
I'm trying to write a simple command that will perform search/replace for a word in all html (*.htm,*.html) files in all sub-folders.

I wrote this command, but it will only search within folder:

perl -pe 's/<search>/<replace>/g' *.htm*

Thanks,
0
Comment
Question by:maheflin25
  • 2
3 Comments
 
LVL 76

Expert Comment

by:arnold
ID: 34908874
perl -i.bak -p -e 's/searchpattern/replacementpattern/'  *.html */*.html.
another option is to use find
find . -name "*.hml" | while read a; do
perl -i.bak -pe 's/<search>/<replace>/g' $a
done
The -i.bak will create a backup file of the one being changed and saved as file.html.bak

find . -name "*.html" -exec perl -i.bak -ep 's/pattern/replacemenet/g' {}\;
could work provided there aren't too many files.

0
 

Author Comment

by:maheflin25
ID: 34909263
Thanks for your solution, however it does not cover files in its subdirectories.

When I tried this command: find . -name "*.html" -exec perl -i.bak -ep 's/pattern/replacemenet/g' {}\;

I get an error message: find: missing argument to '-exec'

Regards,
0
 
LVL 76

Accepted Solution

by:
arnold earned 500 total points
ID: 34909920
find goes through the directory structure.
is perl in your path?
which perl/
replace perl with /usr/bin/perl

alterantively, you can do
find . -name "*.html" | xargs perl -i.bak -ep 's/pattern/replacemenet/g'
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now