如何通过一个变量包含斜线的sed [英] how to pass a variable containing slashes to sed

查看:154
本文介绍了如何通过一个变量包含斜线的sed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我其实知道这个问题的答案,但是我不止一次见过一些我的同龄人被困在这个问题上,所以我认为这将是值得SO记录它。如果这个问题已经存在,我会很乐意将其删除。

I in fact know the answer to this question, however more than once I've seen some of my peers stuck on this issue, so I thought it would be worthwhile to document it on SO. If the question already exists, I'll gladly delete it.

所以这里去...

你如何通过包含斜线作为图案的变量 SED

How do you pass a variable containing slashes as a pattern to sed.

例如,如果我有以下变量:

For example, if I have the following variable:

var="/Users/Documents/name/file"

我想将它传递给 SED 像这样:

sed "s/$var/replace/g" $file

但是我得到的错误。我怎样才能绕过这个问题?

However I get errors. How can I circumvent the issue?

推荐答案

使用替代正则表达式的分隔符为 SED 允许你使用任何分隔符(包括控制字符

Use an alternate regex delimiter as sed allows you to use any delimiter (including control characters):

sed "s~$var~replace~g" $file

这篇关于如何通过一个变量包含斜线的sed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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