用静态值替换XML标签值匹配的前两个字符

发布于 2025-02-01 05:32:33 字数 1010 浏览 5 评论 0原文

我正在尝试替换XML TAG的前两个字符< linestyle>< color> value与00如果模式匹配不是

xml

<?xml version="1.0" encoding="UTF-8"?>
  <LineStyle>
    <color>ff969696</color>
  </LineStyle>
    <color>ff969696</color>
  <LineStyle>
    <color>e680e680</color>
  </LineStyle>
    <color>e680e680</color>
  <LineStyle>
    <color>e680f7f7</color>
  </LineStyle>
    <color>e680f7f7</color>
  <LineStyle>
    <color>e67c88f4</color>
  </LineStyle>
    <color>e67c88f4</color>

ex。,&lt; linestyle&gt;&lt; color&gt; ff ......&lt; linestyle&gt; e6 ......将用&lt; linestyle&gt;&gt; 00 ......等等。

尝试的代码

sed 's/<LineStyle><color>[0-9][a-z]*/<LineStyle><color>00*/g'

I am trying to replace the first two characters of an XML tag <LineStyle><color> value match with 00 if the pattern match isn't 00.

XML

<?xml version="1.0" encoding="UTF-8"?>
  <LineStyle>
    <color>ff969696</color>
  </LineStyle>
    <color>ff969696</color>
  <LineStyle>
    <color>e680e680</color>
  </LineStyle>
    <color>e680e680</color>
  <LineStyle>
    <color>e680f7f7</color>
  </LineStyle>
    <color>e680f7f7</color>
  <LineStyle>
    <color>e67c88f4</color>
  </LineStyle>
    <color>e67c88f4</color>

Ex., a pattern match of <LineStyle><color>ff...... or <LineStyle><color>e6...... would be replaced with <LineStyle><color>00...... and so on.

Attempted Code

sed 's/<LineStyle><color>[0-9][a-z]*/<LineStyle><color>00*/g'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不疑不惑不回忆 2025-02-08 05:32:33

使用此perl单线:

perl -i.bak -0777 -pe 's{( <LineStyle> \s* <color> ) .. }{${1}00}gxms' in_file

perl单线使用以下命令行标志:
-e:告诉Perl在线寻找代码,而不是在文件中。
-p:一次在输入一行上循环,将其分配给$ _默认情况下。添加打印$ _在每个循环迭代后。
-i.bak:在现场编辑输入文件(覆盖输入文件)。在覆盖之前,通过将其名称附加到Extension .bak来保存原始文件的备份副本。
-0777:全部slurp文件。

正则使用这些修饰符:
/g:反复匹配模式。
/x:忽略空格和评论,以获取可读性。
/m:允许多行匹配。
/s:允许匹配新线。

另请参见:
perldoc perlrun
perldoc perllre:perl Parrications(Regexes)

perldoc perlrequick:perl Prinestions快速启动

Use this Perl one-liner:

perl -i.bak -0777 -pe 's{( <LineStyle> \s* <color> ) .. }{${1}00}gxms' in_file

The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-p : Loop over the input one line at a time, assigning it to $_ by default. Add print $_ after each loop iteration.
-i.bak : Edit input files in-place (overwrite the input file). Before overwriting, save a backup copy of the original file by appending to its name the extension .bak.
-0777 : Slurp files whole.

The regex uses these modifiers:
/g : Match the pattern repeatedly.
/x : Ignore whitespace and comments, for readability.
/m : Allow multiline matches.
/s : Allow . to match a newline.

SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches
perldoc perlre: Perl regular expressions (regexes)
perldoc perlrequick: Perl regular expressions quick start

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文