2014-12-03 58 views
0

我想要實現的是,使用Shell腳本將具有修改標記的html標記替換爲文件。當我使用Shell搜索和替換時,我開始瞭解SED。我嘗試了我的目的,但它會拋出錯誤。我的代碼,在Shell中使用SED查找並替換HTML標記

pattern='<html style=background-color:#ffffff;>' 
replacement='<html style=background-color:#ffffff; manifest="app.appcache">' 

cat "index.html" | sed "/s/$pattern/$replacement/" > "index2.html" 

我得到下面的錯誤,

sed: 1: "/s/<html style=backgrou ...": invalid command code < 

從其他一些計算器問題,我試着不使用

pattern='<html style=background-color:#ffffff;>' 
replacement='<html style=background-color:#ffffff; manifest="app.appcache">' 

sed "/s/$pattern/$replacement/" <"index.html" >"index2.html" 

我得到了同樣的錯誤。

請幫助我如何做到這一點。

回答

2

嘗試

# v-- no leading slash 
sed "s/$pattern/$replacement/" index.html > index2.html 
1

應該這樣做:

pattern="<html style=background-color:#ffffff;>" 
replacement="<html style=background-color:#ffffff; manifest=\"app.appcache\">" 

sed "s/$pattern/$replacement/g" index.html > index2.html 
+1

如果用''s替換模式周圍的''s,則模式內部的'''將被shell丟棄。 – Wintermute 2014-12-03 08:18:48

0

感謝您的答案, 它的工作用 '@' 這是我從一個博客了

sed -e '[email protected]<html style=background-color:#ffffff;>@<html style=background-color:#ffffff; manifest="app.appcache">@g' <"index.html" >"index2.html" 
+0

錯誤不是'@'。看看Wintermute的答案。唯一的問題是主導的斜線。 – whoan 2014-12-03 11:53:23