Question

I am trying to create a script that replaces sub strings with an absolute path, so I'm using

sed s/TEMPDIR/$PWD/ file.py > newfile.py

So obviously I want to replace TEMP with the absolute path, but I keep getting

sed: -e expression #1, char 12: unknown option to `s'

I think it might be because the path contains '/', but I don't know how to solve it. Any ideas?

Also, deos PWD guaranties an absolute path, given that I don't deal with links?

Thanks!!

Was it helpful?

Solution

use a different delimiter:

sed "s@TEMPDIR@$PWD@" file.py > newfile.py

Using PWD probably does not guarantee an absolute path. (Depends on the shell). But pwd will return an absolute path, so use:

sed "s@TEMPDIR@$(pwd)@" file.py > newfile.py

Note that neither of these will work if your paths contain @. Pick a delimiter that does not appear in either. Note the following from the shell standard:

In cases where PWD is set to the pathname that would be output by pwd -P, if there is insufficient permission on the current working directory, or on any parent of that directory, to determine what that pathname would be, the value of PWD is unspecified. Assignments to this variable may be ignored. If an application sets or unsets the value of PWD , the behaviors of the cd and pwd utilities are unspecified.

So you can probably rely on $PWD. Note that neither pwd nor $PWD will resolve symbolic links, so you might prefer to use readlink -f .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top