开发者

Xmlstarlet and sed to replace string in a file

I have huge number of html files. I need to replace all the , and " with html entities &nsbquo and &quto respectivel开发者_运维问答y. I need to succeed in two steps for this: 1) Find all the text between

tags. I need to replace only in this text between

tags. 2) Replace all required strings using sed

My command for this is : xmlstarlet sel -t -v "*//p" "index.html" | sed 's/,/\&nsbquo/'

This works, but now I dont know how to put back the changes to index.html file. In sed we have -i option, but for that I need to specify the filename with sed command. But in my case, i have to use | to filter out the required string from html file.

Please help. I did a lot of search for this from 2 days but no luck.

Thank you, Divya.


The main problem here is that in XML there is no difference between " and ", so you can't use xmlstarlet to do this directly. You could replace " with a special string and then use sed to replace that with ":

xmlstarlet ed -u "//p/text()" \
    -x "str:replace(str:replace(., ',', '@NSBQUO@'), '\"', '@QUOT@')" \
    quote.html | \
  sed 's/@NSBQUO@/\&nsbquo\;/g; s/@QUOT@/\&quot\;/g' > quote-new.html
mv quote-new.html quote.html

NOTE: str:replace and other exslt functions were only added to xmlstarlet ed in version 1.3.0, so it was not available at the time this question was asked.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜