简体   繁体   中英

Find and Replace HTML tag using SED in Shell

What I want to achieve is, replace a html tag with modified tag in file using Shell script. When I googled for find and replace using Shell, I came to know about SED. I tried for my purpose, but it throws the error. My code is,

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

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

I'm getting the below error,

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

From few other stackoverflow questions, I tried without using

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

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

I'm getting the same error.

Please help me how to do this.

Try

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

It should be done like this:

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

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

感谢您的回答,它正在与我从博客中获得的“ @”一起使用

sed -e 's@<html style=background-color:#ffffff;>@<html style=background-color:#ffffff; manifest="app.appcache">@g' <"index.html" >"index2.html"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM