For those of you who occasionally need to do some heavy lifting with lots of text, the following is for you.

Now, I’ve got a pile of handy one-liners that I’ve been accumulating over the years, but I have to say this is probably one of the more useful and concise ones out there.

find . ! -path '*/.svn*' -name '*.htm*' -type f
-exec perl -p -i -e 'undef $/; s/<!-- SiteCatalyst.*<!-- End SiteCatalyst code version: G.9. -->//ms' {} \;

I had to parse through a pile of legacy pages on the vancouver.com site today to remove some 3rd party code. Simple find and replace is generally quite easy using sed, but this time around i found sed was getting in the way.

In comes Perl… I swear, any developer out there that does not know Perl must have a bitch of a time.

Breaking it down:

find . ! -path '*/.svn*' -name '*.htm*' -type f
find me all *.htm files, excluding the .svn directories
-exec perl -p -i -e 'undef $/; s/<!-- SiteCatalyst.*<!-- End SiteCatalyst code version: G.9. -->//ms' {} \;
do the dirty work…. substitution happens here

1504 files edited.

I’m feeling a tad more dangerous now.

Comments are closed.