我需要过滤一个大文件XML并使用多个条件查找字符串。我需要一个过滤电子邮件,如果cnisfCF等于真和natg_passwordAlreadyResetedPostMigration是真。
有人能帮忙吗?
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>发布于 2022-11-02 23:36:27
在测试运行以下命令之前,我冒昧地将缺失的结束标记添加到数据中,并假定cnisfCF是指natg_cfIsCn (属性和节点名称区分大小写)。
使用xmlastarlet:
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_cfIsCn和natg_passwordAlreadyResetedPostMigration的custom-attributes/custom-attribute子节点的profile节点的值,并分别为false和true值。
这里的棘手之处在于以可读的方式表示命令,因为路径中涉及的节点名称太长了。我解决了这个问题,首先匹配//profile路径,然后执行两个独立的步骤,从那里缩小结果集。
只使用单数“值-of”的XPath查询的select语句如下所示
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如果这个看起来更漂亮,那就用它代替。我相信它们应该是等同的。
注意,上面的命令不会为给定的文档产生任何输出,因为没有匹配查询的数据。
https://unix.stackexchange.com/questions/723448
复制相似问题