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).

link|improve this question

feedback

1 Answer

up vote 1 down vote accepted

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.)

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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