[SPAM] Re: need help with reinplace

Helmut K. C. Tessarek tessarek at evermeet.cx
Sun Nov 4 05:26:10 UTC 2018


On 2018-11-03 21:48, Joshua Root wrote:
> This example misses the point because the command does not contain any
> characters that have a special meaning in Tcl. The only reason you
> needed to use subst is because you had a variable you wanted substituted
> *and* had other special characters that you didn't want substituted.

You are right, although the s/good/bad/g was just meant as a placeholder
for a regex.
And let's be honest, a "real" regex usually entails character classes,
back references, and all the stuff which makes us use regexes in the
first place.

I know that escaping and quoting can always be a mess.

It makes sense to use tcl variables in the regex, thus I believe the
above example could be of help for not tcl experts (like me).
We could replace the {s/bad/awesome/g} with {YOUR_REGEX_HERE}.
The fact that YOUR_REGEX_HERE can be any regex that is understood by all
regex engines makes this example worthwhile. You don't have to worry
about escaping, quoting, or whatnot. Just add a regex and it works.

> This works fine: reinplace -E "s/bad/awesome/g" FILE
> 
> So does this: reinplace -E "s/bad/${good}/g" FILE
> 
> And this: reinplace -E {s/\[bad\]/good/g} FILE

Right, but all these examples also work without the -E option.
In this case it's not a regex, but a regular substitution.

> It's only when you have those literal brackets *and* you want ${good} to
> be treated as a Tcl variable that you need a different approach. And you
> have exactly this problem running sed from a shell, just with a
> different set of characters that need protecting. Single quotes in the
> shell are very similar to Tcl's braces, stopping parameters from being
> expanded.

This works perfectly on the commandline without additional escaping:

cat configure.ac |sed -E "s/AC_INIT\(\[tmux\],
(next-[0-9]+\.[0-9]+)\)/AC_INIT([tmux], \1 (${version}))/g"

(with export version=bla)

But I get your point. There are cases where additional escaping is
required. Still, IMO this additional escaping makes sense, while in the
reinplace case it does not. I was trying for hours to correctly escape
in reinplace.

> Note that you could also go another layer deep into backslash hell to
> solve this problem, but I really try to avoid recommending that.

Right, I think I've tried unsuccessfully for hours, before I gave up and
posted my failure in this list. ;-)

Cheers,
  K. C.

-- 
regards Helmut K. C. Tessarek              KeyID 0x172380A011EF4944
Key fingerprint = 8A55 70C1 BD85 D34E ADBC 386C 1723 80A0 11EF 4944

/*
   Thou shalt not follow the NULL pointer for chaos and madness
   await thee at its end.
*/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <http://lists.macports.org/pipermail/macports-dev/attachments/20181104/21c29cc0/attachment-0001.sig>


More information about the macports-dev mailing list