假设我有以下类型的文件:
<?xml version="1.0" encoding="utf-8"?> <preferences> <section id="widgets"> <value id="version" xml:space="preserve">1</value> </section> <section id="wuid-b2a8e6b8-6619-714e-9cfe-466c27c90902"> <value id="path to widget data" xml:space="preserve">{Preferences}widgets/opera-adblock-1.3.4-1.oex</value> </section> <section id="wuid-0c5cfdb2-8e51-f149-a1e7-51d66240ed7a"> <value id="path to widget data" xml:space="preserve">{Preferences}widgets/flag-button-1.5.4-1.oex</value> </section> </preferences>我的任务是在最后一次出现</section>之后添加文本。
看看这两个,似乎使用tac会更简单,但我不明白如何做到这一点: 使用sed将字符串附加到模式的第四个出现处 , http://www.unix.com/unix -dummies疑问的答案/ 46294-附加线后最后occurnace-pattern.html#post302149709
谢谢。
Let's say I have the following kind of file:
<?xml version="1.0" encoding="utf-8"?> <preferences> <section id="widgets"> <value id="version" xml:space="preserve">1</value> </section> <section id="wuid-b2a8e6b8-6619-714e-9cfe-466c27c90902"> <value id="path to widget data" xml:space="preserve">{Preferences}widgets/opera-adblock-1.3.4-1.oex</value> </section> <section id="wuid-0c5cfdb2-8e51-f149-a1e7-51d66240ed7a"> <value id="path to widget data" xml:space="preserve">{Preferences}widgets/flag-button-1.5.4-1.oex</value> </section> </preferences>My mission is to add text right after the last occurrence of </section>.
Looking at these two it seems as if utilizing tac would be simpler but I don't understand how to do that either: Using sed to append a string to the fourth occurrence of a pattern, http://www.unix.com/unix-dummies-questions-answers/46294-add-line-after-last-occurnace-pattern.html#post302149709
Thanks.
最满意答案
在第一次出现字符串之前添加东西比较容易:
sed '/<\/preferences>/i\ADD SOME TEXT\nADD SOME MORE TEXT' file结果:
<?xml version="1.0" encoding="utf-8"?> <preferences> <section id="widgets"> <value id="version" xml:space="preserve">1</value> </section> <section id="wuid-b2a8e6b8-6619-714e-9cfe-466c27c90902"> <value id="path to widget data" xml:space="preserve">{Preferences}widgets/opera-adblock-1.3.4-1.oex</value> </section> <section id="wuid-0c5cfdb2-8e51-f149-a1e7-51d66240ed7a"> <value id="path to widget data" xml:space="preserve">{Preferences}widgets/flag-button-1.5.4-1.oex</value> </section> ADD SOME TEXT ADD SOME MORE TEXT </preferences>你可以阅读更多关于如何在字符串前面插入一行的信息 。 HTH。
It's easier to add things before the first occurrence of a string:
sed '/<\/preferences>/i\ADD SOME TEXT\nADD SOME MORE TEXT' fileResults:
<?xml version="1.0" encoding="utf-8"?> <preferences> <section id="widgets"> <value id="version" xml:space="preserve">1</value> </section> <section id="wuid-b2a8e6b8-6619-714e-9cfe-466c27c90902"> <value id="path to widget data" xml:space="preserve">{Preferences}widgets/opera-adblock-1.3.4-1.oex</value> </section> <section id="wuid-0c5cfdb2-8e51-f149-a1e7-51d66240ed7a"> <value id="path to widget data" xml:space="preserve">{Preferences}widgets/flag-button-1.5.4-1.oex</value> </section> ADD SOME TEXT ADD SOME MORE TEXT </preferences>You can read more about how to insert a line before a string here. HTH.
更多推荐
发布评论