Solved

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

Posted on 2011-02-16
3
382 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 77

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 77

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using sort and uniq to pare down large syslog 6 52
If condition on Html with Asp 11 28
output of netstat -r in Powersshell HTML 3 28
IF statment In Powershell 12 17
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

830 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