Sed:最后一次出现后追加(Sed: append after last occurrence)

编程入门 行业动态 更新时间:2024-10-25 12:18:43
Sed:最后一次出现后追加(Sed: append after last occurrence)

假设我有以下类型的文件:

<?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' file

Results:

<?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.

更多推荐

本文发布于:2023-04-27 23:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329648.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Sed   append   occurrence

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!