简体   繁体   English

使用sed将2个变量与正斜杠一起替换到一个变量位置

[英]use sed to replace 2 variables into one variable place along with a forward slash

i am trying to replace 2 variables with / in-between 2 variables but its giving sed expression -e error 我试图用/在2个变量之间替换2个变量,但是它给sed表达式-e错误

 sed 's/'$presentnetwork'/'$netmask\/$subnet'/g' -i /etc/snmp/snmpd.conf

How to put the / and also substitute a variable using sed even if i try to put only one variable it is also not working 即使我尝试仅放置一个变量,如何使用sed放置/并替换变量也无法正常工作

 sed 's/'$presentnetwork'/'$netmask\/'/g' -i /etc/snmp/snmpd.conf

Is it possible to use escape sequence and variable in the same sed command? 是否可以在同一sed命令中使用转义序列和变量?

You need to wrap it with double quotes " : 您需要用双引号"包裹"

sed "s/$presentnetwork/$netmask\//g" -i /etc/snmp/snmpd.conf
#   ^                              ^

Example: 例:

$ cat a.html 
hello
$ a=hello
$ b=good
$ c=bye
$ sed "s/$a/$b\/$c/g" -i a.html 
$ cat a.html 
good/bye

sed不仅限于/作为分隔符,您还可以使用其他一些字符来使转义更加容易(如果您知道该字符不能成为变量的一部分):

 sed "s_$presentnetwork_$netmask/_g" -i /etc/snmp/snmpd.conf

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

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