2011-12-13 46 views
0

我正在使用Ubuntu Natty。使用Sed正則表達式進行子字符串替換

我正在嘗試使用sed來使用命令行進行字符串替換。

我試圖取代文本文件(app.config)中的部分如下:

%% http is a list of IP addresses and TCP ports that the Riak 
%% HTTP interface will bind. 
{http, [ {"127.0.0.1", 8098 } ]}, 

而且我想更換別的IP地址。這是我的代碼:

ip="192.168.10.12"  
sed -i "s/\(\{http,[\t ]*\[[\t ]*\{\)\"[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\"/\1\"$ip\"/" /root/sandbox/app.config 

但是,我得到一個錯誤sed: -e expression #1, char 103: Invalid preceding regular expression

我在做什麼錯?

在此先感謝。

回答

3

添加正則表達式標誌,編輯正則表達式,從({等關鍵字中刪除轉義。似乎現在的工作

ip="192.168.10.12" 
sed -i -r "s/(\{http,[\t ]*\[[\t ]*\{)\"[1-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\"/\1\"$ip\"/" /root/sandbox/app.config 
+0

我使用-r獲得`'sed:-e表達式#1,字符104:無效引用\ 1''命令的RHS`上面。 msg指向\ 1,但這似乎是合適的。在sed中的錯誤?另外,我們來比較sed版本;-),我的是`GNU sed version 4.2.1`。祝你們好運。 – shellter 2011-12-13 20:47:53

1

這可能會爲你工作:

sed "s/\({http,[\t ]*\[[\t ]*{\)\"[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\"/\1\"$ip\"/" 

你不需要逃避{的時候,他們是文字。