2012-10-26 19 views
1

不更換匹配序列我有這樣的投入在一個特定的文件「sed的」文件

#- seeds: "12.123.233.213,123.231.3.2" 
    - seeds: "12.12.21.21,43.435.54.54" 

正如你所看到的是,種子包含兩個連續的IP地址,所以我想改變IP地址文件。 1號線將不被考慮,因爲它以「#」

所以開始這樣做,我有這樣的:

val=' - seeds: "' 

newSeed=${val}${ip1}','${ip2}'"'  # ---> I'm creating the new seed 
str=`grep "\s- seed" $file`   # ---> finding matching character 
echo $str       # ---> it does print out the 2nd line 
sed -i 's|${str}|${newSeed}|g' $file # --> now replace the matched string with new seed value 

但它不會替換文件中的值。我在這裏做錯了什麼?

回答

2

單引號不擴展變量名稱。使用雙引號。

1

對於bash擴展變量,您需要雙引號。

sed -i "s/${str}/${newSeed}/g"

+0

哦,我錯過了!非常感謝choroba&sudo_o – dreamer