sed和由$变量替换字符串 [英] sed and replace string by a $variable

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

问题描述

我有一个小问题。

我尝试通过在文件中的$变量替换字符串(井线)。

I try to replace a string(well a line) by a $variable in a file.

于是我就用下面的命令:

So I use the command :

sed -i -e "s/conf .*/conf = $PATH_CONF/g" generals.conf

如果PATH_CONF不包含像特价字符/,它的工作。

If PATH_CONF does not contains specials characters like "/", it's working.

但PATH_CONF包含路径(/家/等),所以它有几个/,然后我得到了一个错误:

But PATH_CONF contains a path(/home/etc.) so it has several "/", then I got an error :

 bad flag in substitute command: 'h' 

那么,如何,我必须做的有特色的字符在我的$变量?

So how must I do to have specials characters in my $variable ?

感谢。

推荐答案

尝试:

sed -i -e "s@conf .*@conf = $PATH_CONF@g" generals.conf

您需要相同的字符作为分隔符(在这里@),而不是三个/ s的必然。

You need the identical chars as separators (here @), not three /s necessarily.

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

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