Yanwar Purnama
Yanwar Purnama

Reputation: 43

sed command replace string with string in file path

I want to change some string in file with content in another file

sed -i "s/##END_ALL_VHOST##/r $SERVERROOT/conf/templates/$DOMAIN.conf/g" $SERVERROOT/conf/httpd_config.conf

I need to change string ##END_ALL_VHOST## with content in file httpd_config.conf

Please help :)

Upvotes: 0

Views: 387

Answers (2)

Ed Morton
Ed Morton

Reputation: 204731

sed cannot operate on literal strings so it's the wrong tool to use when you want to do just that, as in your case. awk can work with strings so just use that instead:

awk '
BEGIN { old="##END_ALL_VHOST##"; lgth=length(old) }
NR==FNR { new = (NR>1 ? new ORS : "") $0; next }
s = index($0,old) { $0 = substr($0,1,s-1) new substr($0,s+lgth) }
' "$SERVERROOT/conf/templates/$DOMAIN.conf" "$SERVERROOT/conf/httpd_config.conf"

You may need to swap the order of your 2 input files, it wasn't clear from your question.

Upvotes: 1

keithpjolley
keithpjolley

Reputation: 2273

Here's a way of doing it. Fancify the cat command as needed.

(pi51 591) $ echo "bar" > /tmp/foo.txt
(pi51 592) $ echo "alpha beta gamma" | sed "s/beta/$(cat /tmp/foo.txt)/"
alpha bar gamma

Upvotes: 1

Related Questions