当前位置: 首页 > 面试题库 >

sed异常/如果删除XML上的单词,则为其他条件

诸葛煜
2023-03-14
问题内容

我当前正在使用sed脚本:

cd(根文件夹)优先

find . -name pom.xml | xargs sed -i "/<dependencies>/,/'<\/dependencies>'/s/-SNAPSHOT//"

当前,此脚本在标记下删除-SNAPSHOT所有pom.xml包含其子文件夹的文件夹上的,<dependencies></dependencies>xml的示例为:

 <parent>
    <groupId>com.techstack.scheduler</groupId>
    <artifactId>scheduler-service</artifactId>
    <version>0.0.9-SNAPSHOT</version>
 </parent>

    <artifactId>scheduler-webapp</artifactId>
    <packaging>war</packaging>
    <name>Scheduler Service Web Application</name>
    <url>http://maven.apache.org</url>

<dependencies>
    <dependency>
        <groupId>com.infor.techstack.scheduler</groupId>
        <artifactId>scheduler-service-core</artifactId>
        <version>0.0.9-SNAPSHOT</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.12-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.security.authentication</groupId>
        <artifactId>oauth10a-client</artifactId>
        <version>0.0.26-SNAPSHOT</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>xerces</artifactId>
                <groupId>xerces</groupId>
            </exclusion>
        </exclusions>
 </dependency>

所以现在,我需要排除那些带有“ scheduler-service-
core”或基本上是scheduler的标签,因为我不需要解析它,但是我的脚本正在删除它,因为它在依赖项标签下,我该如何排除这一点?“调度程序”一词将发生变化,因为我将在不同的服务上使用它,因此脚本应依赖于该词,因为在使用不同的服务时将对其进行更改。

所需的输出应为:

 <parent>
    <groupId>com.techstack.scheduler</groupId>
    <artifactId>scheduler-service</artifactId>
    <version>0.0.9-SNAPSHOT</version>
 </parent>

    <artifactId>scheduler-webapp</artifactId>
    <packaging>war</packaging>
    <name>Scheduler Service Web Application</name>
    <url>http://maven.apache.org</url>

<dependencies>
    <dependency>
        <groupId>com.infor.techstack.scheduler</groupId>
        <artifactId>scheduler-service-core</artifactId>
        <version>0.0.9-SNAPSHOT</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.12</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.security.authentication</groupId>
        <artifactId>oauth10a-client</artifactId>
        <version>0.0.26</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>xerces</artifactId>
                <groupId>xerces</groupId>
            </exclusion>
        </exclusions>
 </dependency>

如果您看到了,则-SNAPSHOTfor artifactID - scheduler-service- core保留,其余的所有依赖项-SNAPSHOT将被删除。


问题答案:

不要尝试使用来编辑XML
sed,它不是针对这种结构化数据而设计的。sed当有人在您原本不希望的空白处插入良性空格时,编辑XML的html" target="_blank">脚本总是会崩溃,而没有编辑XML的人则希望由于布局更改而导致损坏。

相反,我将使用XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- Identity template: just copy everything -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- special rule for version tags that include -SNAPSHOT and whose
       parent tag has an artifactId subtag that contains scheduler-service -->
  <xsl:template match="//version[contains(., '-SNAPSHOT') and not(contains(../artifactId, 'scheduler-service'))]">
    <xsl:copy>
      <!-- copy attributes -->
      <xsl:apply-templates select="@*"/>
      <!-- and only use the part of the node content before -SNAPSHOT -->
      <xsl:value-of select="substring-before(., '-SNAPSHOT')"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

现在您可以使用例如

xsltproc foobar.xsl pom.xml

要么

xalan -in pom.xml -xsl foobar.xsl

根据您喜欢的XSLT处理器,foobar.xsl包含上面的样式表。



 类似资料:
  • 问题内容: 我当前正在使用sed脚本: cd(根文件夹)优先 当前,此脚本在标记下删除了所有文件夹(包括其子文件夹)上的,例如xml: 所以现在,我需要排除的是那些带有“ scheduler-service- core”或基本上是调度程序的标签,因为我不需要解析它,但是我的脚本正在删除它,因为它在依赖项标签下,我该如何排除这一点?“调度程序”一词将发生变化,因为我将在不同的服务上使用此词,因此脚本

  • 我在PowerGrep中使用这个正则表达式,(这个正则表达式搜索字符串实验室拉德TRAN) 搜索和删除包含字符串或部分字符串的纯文本行,效果很好。 现在我需要更多的东西。我想保留单词LABER,但删除所有其他包含LAB的字符串,例如LABOR、LAB1、ALAB、ALABA等。有没有办法“保护”字符串LABER并删除所有其他包含LAB的字符串?试图使用更改上述正则表达式,但它始终包含我需要保留的单

  • 问题内容: 如何将排序集中的一个Redis元素的得分减1,或者如果其值为0,则将其删除?目前,我正在使用(Rails 4.1.4),但得到的是负值。 提前致谢! 问题答案: ZINCRBY的回复是新分数,因此您可以检查它并在小于1时发出ZREM。

  • 我想运行一个调用Java方法并从另一个规则传递事实(或者更确切地说,它的逻辑值)的规则,尽管我不知道java方法是否对这个问题很重要。描述起来并不容易,所以我将尝试基于一个示例来展示它: 这里的问题是,第一条规则并不总是触发,因此不总是定义某些条件,也不计算第二条规则。 我的第二次尝试是创建这样两个独立的规则: 这也不能像预期的那样工作,因为它首先计算someconditionfalse规则,甚至

  • 我有以下elasticsearch索引结构: 其思想是为每个用户id-message\u id插入带有操作“open”的记录,该用户id-message\u id缺少带有操作“open”的记录。为此,我需要获取所有user\u id-message\u id关联,前提是它们没有动作:“open”。 是否可以创建一个查询,返回不同的user_id-message_id记录不包括user_id-mes

  • 我正在编写一个程序,将一些JSON编码的数据存储在一个文件中,但有时生成的文件是空白的(因为没有找到任何新数据)。当程序查找并存储数据时,我执行以下操作: 当然,如果文件是空白的,这将引发一个异常,我可以抓住,但不让我删除文件。我曾经尝试过: 我得到了这个错误: 发生异常时,如何删除文件?