简体   繁体   English

使用sed命令替换一行上的字符串

[英]Replace string on a line using sed command

I have following content in a file 我在文件中有以下内容

return o.baseUrl="http://info.example.com/api/v1/",a.init=t,o.login=e(o.baseUrl+"login",{email:"@email",password:"@password"}

I want to replace info.example.com with api.example.com 我想更换info.example.comapi.example.com

Currently I am using 目前我正在使用

sed -i "s/^info\.example\.com.*$/api.example.com/g" /var/html/www/index.js

But this is not working. 但这是行不通的。 I don't know much about regular expression. 我对正则表达式了解不多。 Can somebody help me on this ? 有人可以帮我吗?

sed您可以提供要替换的确切表达式。

sed 's/info\.example.com/api.example.com/g' file

^ anchor matches info\\.example\\.com at the beginning of the lines. ^锚在行的开头匹配info\\.example\\.com

Anchors are useless in your case. 锚对您而言毫无用处。 Remove the ^ and $ from your pattern: 从模式中删除^$

sed -i 's/info\.example\.com/api.example.com/g' /var/html/www/index.js

More about anchors here . 更多关于锚这里

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

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