简体   繁体   English

如何在sed命令中使用变量替换属性文件中的属性值

[英]How to use variable in sed command to replace a property value in a property file

i can able to use the below command to change value of target by means of hard coding. 我可以使用以下命令通过硬编码来更改目标值。

sed -ie s/^target=.*/target=google.com/ url.properties

But if i used variable i am getting error. 但是,如果我使用变量,则会出错。 I dont know how sed commands all working. 我不知道sed命令如何全部工作。 I only needed to set build system thats it. 我只需要设置构建系统即可。

url = google.com
sed -ie s/^target=.*/target=$url/ url.properties

the error is sed: -e expression #1, char 25: unterminated `s' command sed错误:-e表达式#1,字符25:未终止的s命令

The problem may be happening because your URL may contain / which bash interprets as sed syntax, so something like https/www.google.com ends up something like : 由于您的URL可能包含/ ,bash会将其解释为sed语法,因此可能会出现问题,因此https/www.google.com最终会变成:

sed -ie 's/^target=.*/target=https/www.google.com/' url.properties

I will suggest to delimit any special characters to avoid sed to be confused : 我建议定界任何特殊字符,以免sed引起混淆:

url=google.com
url=`echo $url | sed -e "s/\//\\\\\\\\\//g"` # delimits backslash in URL's
sed -ie "s/^target=.*/target=$url/" url.properties

Two problems: 两个问题:

  • You can't have spaces in variable assignments in bash bash中的变量分配中不能有空格
  • You must quote the sed command (and ideally the url too) 您必须引用sed命令(理想情况下也引用URL)

Working example: 工作示例:

url="google.com"
sed -ie "s/^target=.*/target=$url/" url.properties

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

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