首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用sed在XML文件中搜索多个字符串

使用sed在XML文件中搜索多个字符串
EN

Unix & Linux用户
提问于 2022-11-02 20:50:53
回答 1查看 73关注 0票数 0

我需要过滤一个大文件XML并使用多个条件查找字符串。我需要一个过滤电子邮件,如果cnisfCF等于真和natg_passwordAlreadyResetedPostMigration是真。

有人能帮忙吗?

代码语言:javascript
复制
            teste@gmail.com
            true
            
            
        
        
            
            
            <first-name>teste</first-name>
            <second-name/>
            <last-name>name 1</last-name>
            <suffix/>
            <company-name/>
            <job-title/>
            <email>teste@gmail.com</email>
            <phone-home>542926407485</phone-home>
            <phone-business/>
            <phone-mobile/>
            <fax/>
            <birthday>1999-09-12Z</birthday>
            <gender>2</gender>
            <creation-date>2022-09-19T18:34:45.000Z</creation-date>
            <preferred-locale/>
            <custom-attributes>
                <custom-attribute attribute-id="natg_Newsletter">false</custom-attribute>
                <custom-attribute attribute-id="natg_cfIsCn">false</custom-attribute>
                <custom-attribute attribute-id="natg_cpf">5465465456456</custom-attribute>
                <custom-attribute attribute-id="natg_infContOptIn">false</custom-attribute>
                <custom-attribute attribute-id="natg_optInWP">false</custom-attribute>
                <custom-attribute attribute-id="natg_passwordAlreadyResetedPostMigration">true</custom-attribute>
                <custom-attribute attribute-id="natg_personNumber">116864397</custom-attribute>
                <custom-attribute attribute-id="natg_pushOptIn">false</custom-attribute>
                <custom-attribute attribute-id="natg_rut">456456456</custom-attribute>
            </custom-attributes>
        </profile></code></pre>
EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2022-11-02 23:36:27

在测试运行以下命令之前,我冒昧地将缺失的结束标记添加到数据中,并假定cnisfCF是指natg_cfIsCn (属性和节点名称区分大小写)。

使用xmlastarlet

代码语言:javascript
复制
xmlstarlet select --template \
    --match '//profile' \
    --match 'self::node()[custom-attributes/custom-attribute[@attribute-id="natg_cfIsCn"]="true"]' \
    --match 'self::node()[custom-attributes/custom-attribute[@attribute-id="natg_passwordAlreadyResetedPostMigration"]="true"]' \
    --value-of 'email' -nl file.xml

上面的命令将提取输入文档中任何具有email属性natg_cfIsCnnatg_passwordAlreadyResetedPostMigrationcustom-attributes/custom-attribute子节点的profile节点的值,并分别为falsetrue值。

这里的棘手之处在于以可读的方式表示命令,因为路径中涉及的节点名称太长了。我解决了这个问题,首先匹配//profile路径,然后执行两个独立的步骤,从那里缩小结果集。

只使用单数“值-of”的XPath查询的select语句如下所示

代码语言:javascript
复制
xmlstarlet select --template \
    --value-of '//profile[
        custom-attributes/custom-attribute[@attribute-id="natg_cfIsCn"]="true" and 
        custom-attributes/custom-attribute[@attribute-id="natg_passwordAlreadyResetedPostMigration"]="true"
    ]/email' -nl file.xml

如果这个看起来更漂亮,那就用它代替。我相信它们应该是等同的。

注意,上面的命令不会为给定的文档产生任何输出,因为没有匹配查询的数据。

票数 1
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/723448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档