【问题标题】:strip out xml tags inside placeholders去除占位符内的 xml 标签
【发布时间】:2014-12-15 16:04:30
【问题描述】:

我想使用 sed(或其他工具)去除 xml 标记,但仅限于特定位置,并用 '{{' '}}' 占位符标记。 示例:

<ok><ok2>{{TextShouldStay<not_ok>this_should_be_out</not_ok>
<sthelse/>ThisShouldBeAgain}}</ok2></ok>

预期结果:

<ok><ok2>{{TextShouldStayThisShouldBeAgain}}</ok2></ok>

任何想法如何实现这一目标?

【问题讨论】:

  • {{}} 块是否包含换行符?你想要 Perl 回答吗?
  • @AvinashRaj:没有换行符,Perl 答案也可以!

标签: xml replace sed placeholder strip


【解决方案1】:

命令:

tr '\n' ' ' < file.xml | sed -r 's/(.*\{\{)([A-Za-z0-9]*)(<.*\/>)(.*)/\1\2\4\n/g'

输出:

sdlcb@Goofy-Gen:~/AMD$ cat file.xml
<ok><ok2>{{TextShouldStay<not_ok>this_should_be_out</not_ok>
<sthelse/>ThisShouldBeAgain}}</ok2></ok>
sdlcb@Goofy-Gen:~/AMD$ tr '\n' ' ' < file.xml | sed -r 's/(.*\{\{)([A-Za-z0-9]*)(<.*\/>)(.*)/\1\2\4\n/g'
<ok><ok2>{{TextShouldStayThisShouldBeAgain}}</ok2></ok>
sdlcb@Goofy-Gen:~/AMD$


Here we remove the newlines first using 'tr' and then group the patterns using '(' and ')'. 
First group - from beginning of line to '{{' inclusive
Second group - after '{{', whatever alphabets/numbers
Third group - characters between the next '<' and last '/>'
Fourth group - remaining characters.

Once grouped, we remove the 3rd pattern group, also add newline.

【讨论】:

    猜你喜欢
    • 2013-03-24
    • 2013-03-30
    • 2020-09-21
    • 2011-07-31
    • 2015-12-03
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    相关资源
    最近更新 更多