content
macname@localhost desktop %macname@localhost desktop %cat dddthis
isa test of the test script.
this
isthe second test of the test script.
macname@localhost desktop %
#在行中替換文字
macname@localhost desktop % sed 's/test/trial/
'ddd
this
isa trial of the test script.
this
isthe second trial of the test script.
macname@localhost desktop %
#指定sed編輯器用新文字替換第幾處模式匹配的地方
macname@localhost desktop %macname@localhost desktop % sed 's/test/trial/2
'ddd
this
isa test of the trial script.
this
isthe second test of the trial script.
macname@localhost desktop %
將替換標記指定為2的結果就是:sed編輯器只替換每行中第二次出現的匹配模式。
g替換標 記使你能替換文字中匹配模式所匹配的每處地方
macname@localhost desktop %macname@localhost desktop % sed 's/test/trial/g
'ddd
this
isa trial of the trial script.
this
isthe second trial of the trial script.
macname@localhost desktop %
p替換標記會列印與替換命令中指定的模式匹配的行。這通常會和sed的-n選項一起使用。
-n選項將禁止sed編輯器輸出。但p替換標記會輸出修改過的行。將二者配合使用的效果就是 只輸出被替換命令修改過的行
macname@localhost desktop %cat dddthis
isa test of the test script.
this
isthe second test of the test script.
dededede
hahahahhahahaah
macname@localhost desktop %macname@localhost desktop % sed -n '
s/test/trial/p
'ddd
this
isa trial of the test script.
this
isthe second trial of the test script.
macname@localhost desktop %
w替換標記會產生同樣的輸出,不過會將輸出儲存到指定檔案中
macname@localhost desktop %cat dddthis
isa test of the test script.
this
isthe second test of the test script.
dededede
hahahahhahahaah
macname@localhost desktop % sed '
s/test/trial/w test.txt
'ddd
this
isa trial of the test script.
this
isthe second trial of the test script.
dededede
hahahahhahahaah
macname@localhost desktop %cat test.txt
this
isa trial of the test script.
this
isthe second trial of the test script.
macname@localhost desktop %
sed編輯器允許選擇其他字元來作為替換命令中的字串分隔符,這裡感嘆號被用作字串分隔符
macname@localhost desktop %cat dddthis
isa test of the test script.
this
isthe second test of the test script.
dededede
hahahahhahahaah
macname@localhost desktop %macname@localhost desktop % sed -n '
s!test!trial!p
'ddd
this
isa trial of the test script.
this
isthe second trial of the test script.
macname@localhost desktop %
在這個例子中,感嘆號被用作字串分隔符,這樣路徑名就更容易閱讀和理解了。
macname@localhost desktop %macname@localhost desktop % sed 's!/bin/bash!/bin/csh!
' /etc/passwd
字串替換
描述輸入乙個字串,以回車結束 字串長度 100 該字串由若干個單詞組成,單詞之間用乙個空格隔開,所有單詞區分大小寫。現需要將其中的某個單詞替換成另乙個單詞,並輸出替換之後的字串。輸入輸入包括3行,第1行是包含多個單詞的字串 s,第2行是待替換的單詞a,長度 100 第3行是a將被替換的單詞b。長度 ...
字串替換
一 問題 函式宣告如下 char strreplace char str,char sub,char rep 其中str為原字串,sub為待被替換的子串。為簡單起見,假定字串sub和rep長度一樣 二 源 char strreplace char str,char sub,char rep if f...
字串替換
請你實現乙個簡單的字串替換函式。原串中需要替換的佔位符為 s 請按照引數列表的順序一一替換佔位符。若引數列表的字元數大於佔位符個數。則將剩下的引數字元新增到字串的結尾。給定乙個字串a,同時給定它的長度n及引數字元陣列arg,請返回替換後的字串。保證引數個數大於等於佔位符個數。保證原串由大小寫英文本母...