简体   繁体   English

sed命令用文件路径中的字符串替换字符串

[英]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 我需要使用文件httpd_config.conf中的内容更改字符串## END_ALL_VHOST ##

Please help :) 请帮忙 :)

Here's a way of doing it. 这是一种方法。 Fancify the cat command as needed. 根据需要幻想cat命令。

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

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. sed无法对文字字符串进行操作,因此在您要执行此操作时(例如您的情况),它是使用错误的工具。 awk can work with strings so just use that instead: awk可以使用字符串,因此只需使用它即可:

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. 您可能需要交换2个输入文件的顺序,但您的问题尚不清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM