これはトリックを行います:
port=8787
url="my.domain.com"
url2="my.realdomain.com"
sed -i.bk -Ee "s/(<port>)[0-9]+(<\/port)/\1${port}\2/" \
-e "s/(<service>)[^:]*(:.*)/\1${url}\2/" \
-e "s/localhost/${url2}/" config.sh
出力:
<port>8787</port>
<service>my.domain.com:8190</service>
<wan-access>my.realdomain.com</wan-access>
Regep 説明:
s/ # The first substitution
(<port>) # Match the opening port tag (captured)
[0-9]+ # Match the port number (string of digits, at least one)
(<\/port) # Match the closing port tag (captured, escaped forwardslash)
/ # Replace with
\1 # The first capture group
${port} # The new port number
\2 # The second capture group
s/ # The second substitution
(<service>) # Match the opening service tag (captured)
[^:]* # Match anything not a :
(:.*) # Match everything from : (captured)
/ # Replace with
\1 # The first capture group
${url} # The new url
\2 # The second capture group
s/ # The third substitution
localhost # Match the literal string
/ # Replace with
${url2} # The other new url
タグのマッチングはおそらくそれほど冗長である必要はありませんが、初心者にとって理解しやすいのは確かです。
編集:
<service>
ポートを変更したい場合は、これを試してください:
-e "s/(<service>).*(<\/service)/\1${url}:${port}\2/"