Regex 替换XML文件中的字符串

Regex 替换XML文件中的字符串,regex,sed,Regex,Sed,我试图用sed替换XML文件中的字符串,但由于它的正则表达式不支持非贪婪表达式,所以遇到了一个问题 XML示例: <opt> <Node active="yes" file="/home/user/random_filename" last_time="17/07/14-00:02:07" time_in_min="5" /> </opt> 输出: <opt> <Node active="yes" file="new_file_n

我试图用sed替换XML文件中的字符串,但由于它的正则表达式不支持非贪婪表达式,所以遇到了一个问题

XML示例:

<opt>
  <Node active="yes" file="/home/user/random_filename" last_time="17/07/14-00:02:07" time_in_min="5" />
</opt>
输出:

<opt>
   <Node active="yes" file="new_file_name" />
</opt>

我应该如何处理它?

使用
“[^”]*”
而不是
“*”

输入:

输出:

<opt>
   <Node active="yes" file="new_file_name" />
</opt>

使用
“[^”]*”
代替
“*”

输入:

输出:

<opt>
   <Node active="yes" file="new_file_name" />
</opt>

使用
“[^”]*”
代替
“*”

输入:

输出:

<opt>
   <Node active="yes" file="new_file_name" />
</opt>

使用
“[^”]*”
代替
“*”

输入:

输出:

<opt>
   <Node active="yes" file="new_file_name" />
</opt>


<代码> > p>这不使用<代码> SED。但是,在操作XML文档时,您可能需要考虑XSLT。对于您的问题,它可能是一个超高的解决方案,但它非常适合使用xml

filter.xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" omit-xml-declaration="yes"/>

<!-- The replacement string is passed as a parameter.
     You can have as many param elements as you need. -->
<xsl:param name="NEW_NAME"/>

<!-- Copy all attributes and nodes... -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!-- ...but when you encounter an element with a file attribute,
     replace it with the value passed in the parameter named $NEW_NAME. -->
<xsl:template match="@file">
    <xsl:attribute name="file">
        <xsl:value-of select="$NEW_NAME"/>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>
结果:

<opt>
    <Node active="yes" file="new_file_name" last_time="17/07/14-00:02:07" time_in_min="5"/>
</opt>


<代码> > p>这不使用<代码> SED。但是,在操作XML文档时,您可能需要考虑XSLT。对于您的问题,它可能是一个超高的解决方案,但它非常适合使用xml

filter.xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" omit-xml-declaration="yes"/>

<!-- The replacement string is passed as a parameter.
     You can have as many param elements as you need. -->
<xsl:param name="NEW_NAME"/>

<!-- Copy all attributes and nodes... -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!-- ...but when you encounter an element with a file attribute,
     replace it with the value passed in the parameter named $NEW_NAME. -->
<xsl:template match="@file">
    <xsl:attribute name="file">
        <xsl:value-of select="$NEW_NAME"/>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>
结果:

<opt>
    <Node active="yes" file="new_file_name" last_time="17/07/14-00:02:07" time_in_min="5"/>
</opt>


<代码> > p>这不使用<代码> SED。但是,在操作XML文档时,您可能需要考虑XSLT。对于您的问题,它可能是一个超高的解决方案,但它非常适合使用xml

filter.xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" omit-xml-declaration="yes"/>

<!-- The replacement string is passed as a parameter.
     You can have as many param elements as you need. -->
<xsl:param name="NEW_NAME"/>

<!-- Copy all attributes and nodes... -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!-- ...but when you encounter an element with a file attribute,
     replace it with the value passed in the parameter named $NEW_NAME. -->
<xsl:template match="@file">
    <xsl:attribute name="file">
        <xsl:value-of select="$NEW_NAME"/>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>
结果:

<opt>
    <Node active="yes" file="new_file_name" last_time="17/07/14-00:02:07" time_in_min="5"/>
</opt>


<代码> > p>这不使用<代码> SED。但是,在操作XML文档时,您可能需要考虑XSLT。对于您的问题,它可能是一个超高的解决方案,但它非常适合使用xml

filter.xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" omit-xml-declaration="yes"/>

<!-- The replacement string is passed as a parameter.
     You can have as many param elements as you need. -->
<xsl:param name="NEW_NAME"/>

<!-- Copy all attributes and nodes... -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!-- ...but when you encounter an element with a file attribute,
     replace it with the value passed in the parameter named $NEW_NAME. -->
<xsl:template match="@file">
    <xsl:attribute name="file">
        <xsl:value-of select="$NEW_NAME"/>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>
结果:

<opt>
    <Node active="yes" file="new_file_name" last_time="17/07/14-00:02:07" time_in_min="5"/>
</opt>

$sed's/\(.*file=“\)[^”]*/\1new\u file\u name/'文件
$sed's/\(.*file=“\)[^”]*/\1new\u file\u name/'文件
$sed's/\(.*file=“\)[^”]*/\1new\u file\u name/'文件
$sed's/\(.*file=“\)[^”]*/\1new\u file\u name/'文件

可能重复的可能重复的可能重复的可能重复的可能重复的漂亮的一个!我会考虑的。我需要改变不止一个参数,这看起来是最方便的方法。谢谢。如果需要,匹配属性xpath表达式可以变得更具体。例如,要仅更改节点元素中的文件属性,请将
match=“@file”
更改为
match=“Node/@file”
。还可以根据文件属性的内容进行匹配。要仅更改以“/home/user/”开头的文件,请使用
match=“Node/@file[以(,“/home/user/”)开头]”
。不错!我会考虑的。我需要改变不止一个参数,这看起来是最方便的方法。谢谢。如果需要,匹配属性xpath表达式可以变得更具体。例如,要仅更改节点元素中的文件属性,请将
match=“@file”
更改为
match=“Node/@file”
。还可以根据文件属性的内容进行匹配。要仅更改以“/home/user/”开头的文件,请使用
match=“Node/@file[以(,“/home/user/”)开头]”
。不错!我会考虑的。我需要改变不止一个参数,这看起来是最方便的方法。谢谢。如果需要,匹配属性xpath表达式可以变得更具体。例如,要仅更改节点元素中的文件属性,请将
match=“@file”
更改为
match=“Node/@file”
。还可以根据文件属性的内容进行匹配。要仅更改以“/home/user/”开头的文件,请使用
match=“Node/@file[以(,“/home/user/”)开头]”
。不错!我会考虑的。我需要改变不止一个参数,这看起来是最方便的方法。谢谢。如果需要,匹配属性xpath表达式可以变得更具体。例如,要仅更改节点元素中的文件属性,请将
match=“@file”
更改为
match=“Node/@file”
。还可以根据文件属性的内容进行匹配。要仅更改以“/home/user/”开头的文件,请使用
match=“Node/@file[以(,“/home/user/”)开头]”