Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-16
3
Medium Priority
?
386 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 79

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 79

Accepted Solution

by:
arnold earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

721 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