[nSLUG] Find and replace command

Ben Armstrong synrg at sanctuary.nslug.ns.ca
Sat Oct 4 08:07:25 ADT 2014

On 04/10/14 07:41 AM, Monte Stevens wrote:
> Trina Conrad <trina.hanson79 at gmail.com> writes:
>> One more question.  When I run the following command:
>> find -iname '*.html' -print -exec sed -i 's/<link href="../CSS/main.css" rel=
>> "stylesheet" type="text/css">/<link href="../../Files/main.css" rel=
>> "stylesheet" type="text/css">/' {} \;
> Replace the substitution delimiter `/' characters with another
> character, such as `}', i.e., `s}<link href'.

Exactly. Also, two minor critiques of the command. First, the -print is
redundant. Second, if you've moved the css file, it's likely *any* path
to that css file needs to be changed, so the expression can be made much
less wordy and work just as well:

find -iname '*.html' -exec sed -i 's}CSS/main\.css}../Files/main.css}' {} \;

If you're observant, you'll notice I quoted the metacharacter '.' (which
means "match any character") as '\.' to match a literal period instead.
In fact, even without quoting it, the expression will still work. This
is just me being fussy. :)

There's a practical reason for making such a lengthy regex shorter
beyond just tidiness.  If you inadvertently introduced a newline or
extra blank into your html file then your wordier regex would fail to
make a match without further changes to allow for one-or-more whitespace
everywhere you currently have a single blank.


More information about the nSLUG mailing list