Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 2 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.)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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