[aklug] Simple Way to Replace text in Source Code?

James Zuelow e5z8652 at zuelow.net
Sun Sep 3 08:37:43 AKDT 2017

On 09/02/2017 06:56 PM, Christopher Howard wrote:
> Hi list, I'm trying to package up a program I wrote in PicoLisp. One
> annoyance is that PicoLisp doesn't have a built-in load-path system
> (and I didn't want to make one right now) meaning that if I need to
> have the path to files hard-coded in when installing to arbitrary
> locations. So, I wanted to throw a command into the Makefile that when
> I run make, it replaces load paths in the source with the $(PREFIX)
> that has been set.
> I've heard of "sed", but the challenge I'm running into there is that
> it seems if I do something like sed s/STUB/$(PREFIX)/, and prefix is
> like /usr/local, then you get sed s/STUB//usr/local/ which messes the
> command up. And escaping the PREFIX before-hand doesn't appeal to me.
> Does anybody have any suggestions for me? I want to keep the
> build/install system as simple as possible, but have to deal with this
> issue
Well this one is easy:  "escaping the prefix before-hand doesn't appeal 
to me."

Remember sed can use alternate markers.  You don't have to use a forward 
slash, which gets hairy when you start escaping stuff.

So for example:

~$ echo "no.prefix" > sed-test

With the normal forward slash markers you need:

~$ sed s/no.prefix/\\/root\\/with.prefix/ sed-test

But change the marker from the forward slash to a hash:

~$ sed s#no.prefix#/root/with.prefix# sed-test

Super easy.  UNLESS, you're also using the hash sign in your expression, 
and now you've got to escape it too...  So pick you alternate marker 

Don't get crazy or you can't read your code.  On my system this also 
works, but I would never recommending using "d" as a marker:

~$ sed sdno.prefixd/root/with.prefixd sed-test


More information about the aklug mailing list