1

I need to create a text file where parts of it should be replaced by any arbitrary path. I wanted to do that by copying a template file and then replace a special pattern, e.g. ${MY_PATH} with the arbitrary path (I hope, no quoting is needed). How to do that replacing part (the sed-examples I saw so far seemed to be problematic because of the arbitrary path).

2

The replacement doesn't need much quoting: only backslashes and the regexp separator. For convenience, the latter can often be chosen arbitrarily - instead of s/.../.../ one could, in many programs, say s|...|...| or similar.

Assuming the path is $path, you could do:

epath=${path//'\'/'\\'}
epath=${epath//'|'/'\|'}
sed "s|\${MY_PATH}|$epath|g" < in > out

Or if you are not afraid of other languages, you could use:

perl -e 'my $path = shift(@ARGV); while (<STDIN>) {s|\$\{MY_PATH\}|$path|g; print}' "$path" < in

(Not sure I got the sample right, but you get the idea.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.