Deprecated: Assigning the return value of new by reference is deprecated in /var/www/mistcat.com/public/gallery2/modules/core/classes/GalleryUtilities.class on line 854 Notice: Undefined index: wpg2_sidebargridblocksimageframe in /var/www/mistcat.com/public/wp-content/plugins/wpg2/wpg2functions.inc on line 556 Notice: Undefined variable: cssappendoutput in /var/www/mistcat.com/public/wp-content/plugins/wpg2/wpg2functions.inc on line 559

Finding Replacing Filenames & text inside files using perl

A few “Bash Tricks” for the peeps

First up:

Find & replace filenames

This finds file that match Somethinghtml.html and renames to Something.html (HTTP Track users will recognize these two examples as fixes to common naming problems when scraping a site.) Note you can change the mask of files searched by changing the find mask at the end of the line.

<code>

perl -p -i -e ‘s/(.*)html\.html/\1.html/g;’ `find ./ -name ‘*.html’`

</code>

Then Find & Replace text inside of files:

This finds things that have been incorrectly prefixed with dev.mistcat.com and removes that prefix so that this 2112.js file can load from a remote domain correctly.  Note you can change the find mask at the beginning to restrict or un-restrict your searched files for text replacement.

<code>

find . -name ‘*.html’ | perl -pi -e ‘s/http:\/\/dev\.mistcat\.com\/(www\.dwin1\.com\/2112\.js)/http:\/\/\
1/g;’

</code>

Coding, Linux / Unix / CLI

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Leave Comment

(required)

(required)