替换sed中具有正斜杠的字符串 [英] Replacing string having forward slash in sed

查看:351
本文介绍了替换sed中具有正斜杠的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望替换

x.y.z.zz=/a/b/c/d/

x.y.z.zz=/a/b/e/d/

我提前知道了x.y.z.zz.我也提前知道了行号. 我已经尝试过

i know x.y.z.zz in advance.I also know the line number in advance. I have tried this

sed  "11s/.*/x.y.z.zz=\/a\/b\/e\/d\/" filename

但这给出了错误.有没有更好的方法直接搜索和替换字符串?

but this is giving error. Is there a better way to directly search and replace the string ?

推荐答案

sed使用sed 's/pattern/replacement/'语法替换.就您而言,您遗漏了最后一个/.因此,这样说就可以了:

sed replaces by using the sed 's/pattern/replacement/' syntax. In your case, you were missing the last /. So by saying this it will work:

sed '11s/.*/x.y.z.zz=\/a\/b\/e\/d\//' file
                                   ^

但是,使用另一个定界符可能会更干净,因此语法更加清晰.那#呢? (也可以是~_等):

However, it may be cleaner to use another delimiter, so that the syntax is more clear. What about #? (It can also be ~, _, etc.):

sed '11s#.*#x.y.z.zz=/a/b/e/d/#' file

测试

$ cat a
a
x.y.z.zz=/a/b/c/d/
b
c

让我们替换第2行:

$ sed '2s#.*#x.y.z.zz=/a/b/e/d/#' a
a
x.y.z.zz=/a/b/e/d/
b
c

这篇关于替换sed中具有正斜杠的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆