2012-01-18 53 views
1

這裏的第一張海報。使用sed查找並替換一系列數字

試圖使用sed來查找和替換單詞。代碼如下:

configFile=ConnectionConfig.xml 
sed 's/30??\" authentication=\"someuniqueauthkey/'$1'\" authentication=\"someuniqueauthkey/' $configFile 

而且我會像這樣的文件:

./choosePort.sh 3001 

當我運行這段代碼,有沒有像壞的正則表達式格式給出的錯誤,它只是不替換任何內容並且tempXml.xml的內容只是ConnectionConfig的內容,不變。

我希望能夠做的是承認任何數字3000 - 3099在30?表達的一部分,我認爲這是'?'沒有。

我試圖改變輸入線:

<host id="0" address="someip" port="3001" authentication="someauthkey" 

在此先感謝。 (出於安全原因,文件中的Ip和authkeys被刪除)。

+0

能否請你把衣服脫你的代碼,直接關係到sed的一部分?你可以請你張貼你的輸入文件的一些部分? – 2012-01-18 11:07:14

+0

完成並完成:) – Jarob22 2012-01-18 11:11:29

+0

什麼是30?應該是這個意思。你想與此相匹配的是什麼? ''''''裏面爲什麼'''''''''''' – 2012-01-18 11:09:38

回答

3

使用[0-9]而不是?來匹配一個數字。

sed 's/30[0-9][0-9]\" authentication=\"someuniqueauthkey/'$1'\" authentication=\"someuniqueauthkey/' $configFile 
1

要匹配一個數字,你可以使用[0-9]\{2}\d\{2}

在你的情況??也將只匹配30

您可以找到正則表達式here的完美概覽。

+0

[0-9] {2 } – Jarob22 2012-01-18 12:06:18

+0

@ Jarob22:jep,對不起,錯過了反斜槓 – ezdazuzena 2012-01-18 13:41:10

1

首先,匹配的表達必須是30..30??
然後,你似乎已經忘記了-e參數sed的。
嘗試使用:

sed -e 's/30..\" authentication=\"someuniqueauthkey/'$1'\" authentication=\"someuniqueauthkey/' $configFile 
+0

其他答案沒有-e,你爲什麼需要它? – Jarob22 2012-01-18 12:00:04

+0

如果你的腳本是單個字符串,那麼'-e'不是必須的。 – tripleee 2012-01-18 12:00:09

0

這可能會爲你工作:

sed 's/30[0-9][0-9]\(" authentication="someuniqueauthkey\)/'$1'\1/' $configFile