簡體   English   中英

sed命令用文件路徑中的字符串替換字符串

[英]sed command replace string with string in file path

我想更改文件中的某些字符串,並將其內容包含在另一個文件中

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

我需要使用文件httpd_config.conf中的內容更改字符串## END_ALL_VHOST ##

請幫忙 :)

這是一種方法。 根據需要幻想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無法對文字字符串進行操作,因此在您要執行此操作時(例如您的情況),它是使用錯誤的工具。 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"

您可能需要交換2個輸入文件的順序,但您的問題尚不清楚。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM