I have a large number of files that I need to do a search and replace on. At this point, I'm resigned to using the following:
find . -name '*.html' -type f -exec perl -pi -e 's/foo/bar/g;' \{\} \;
Which has two problems that I can think of:
Every file acted upon spawns a new process through find's -exec option. I was thinking instead to try:
perl -pi -e 's/foo/bar/g;' `find . -name '*.html' -type f`
But I've had problems with that in the past when find returns a very large list.
Files that do not contain the match get operated on anyway. There's a lot of extra overhead here as well, and timestamps get changed to boot. Again, I could try:
perl -pi -e 's/foo/bar/g;' `find . -name '*.html' -type f -exec grep -l foo \{\} \;`
But I still have all the same problems as with the first item above.
Is there another way to use perl -i on a directory recursively so that only files matching a certain criteria are updated?
elbieelbieelbie
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|