2014-01-22 85 views
0

有人可以告訴我如何可以用部分替換整個字符串嗎? 例如,如果txt文件包含:只用部分匹配替換整個字/數字與SED只有部分匹配

The number of water molecues: h20=20e43 

,我需要它說:

The number of water molecues: h20=10e54 

我迄今爲sed 's/h20/h20=10e54/',它輸出:

The number of water molecues: h20=10e54=20e43 

問題是我想要替換的原始值並不總是相同的,所以我需要它只搜索模式h20=並替換整個事情。

+1

將'sed的「S/H20 = */H20 = 20e43 /'的作品嗎? – cnicutar

回答

1

嘗試:s/\(h20=\)[[:alnum:]]*/\110e54/

匹配 「H 2 O =」(並記住值),隨後加入一些字母數字字符;用記憶的值(\1)加上你想要的字符串替換。

另外:perl -pe 's/h20=\K\w+/10e54/'

0

下自己的想法,這裏是修復。

echo "The number of water molecues: h20=10e54" |sed 's/h20=.*/h20=10e54/' 
+0

哦,cnicutar在4小時前提供了相同的方法。大聲笑。 – BMW

0

awk也可用於:

echo "The number of water molecues: h20=20e43" | awk '{sub(/h20=.*/,"h20=10e54")}1' 
The number of water molecues: h20=10e54