linux - Grep 用于文件中的多个模式

标签 linux shell unix

我想计算我的 xml 文件中的 xml 节点数(grep 或某种方式)。

....
<countryCode>GBR</countryCode>
<countryCode>USA</countryCode>
<countryCode>CAN</countryCode>
...
<countryCode>CAN</countryCode>
<someNode>USA</someNode>
<countryCode>CAN</countryCode>
<someNode>Otherone</someNode>
<countryCode>GBR</countryCode>
...

如何计算各个国家/地区的数量,例如 CAN = 3、美国 = 1、GBR = 2?如果不传入国家名称,可能还会有更多国家?

更新:

除了countrycode还有其他节点

最佳答案

我的简单建议是使用 sortuniq -c

$ echo '<countryCode>GBR</countryCode>
<countryCode>USA</countryCode>
<countryCode>CAN</countryCode>
<countryCode>CAN</countryCode>
<countryCode>CAN</countryCode>
<countryCode>GBR</countryCode>' | sort | uniq -c
      3 <countryCode>CAN</countryCode>
      2 <countryCode>GBR</countryCode>
      1 <countryCode>USA</countryCode>

您将在 grep 而不是 echo 的输出中进行管道传输。一个更健壮的解决方案是使用 XPath。如果您的 XML 文件看起来像

<countries>
  <countryCode>GBR</countryCode>
  <countryCode>USA</countryCode>
  <countryCode>CAN</countryCode>
  <countryCode>CAN</countryCode>
  <countryCode>CAN</countryCode>
  <countryCode>GBR</countryCode>
</countries>

然后你可以使用:

$ xpath -q -e '/countries/countryCode/text()'  countries.xml  | sort | uniq -c
      3 CAN
      2 GBR
      1 USA

我说它更健壮,因为使用专为解析平面文本而设计的工具在处理 XML 时本身就不稳定。根据原始 XML 文件的上下文,不同的 XPath 查询可能会更好,这将在任何地方匹配它们:

$ xpath -q -e '//countryCode/text()'  countries.xml  | sort | uniq -c
      3 CAN
      2 GBR
      1 USA

关于linux - Grep 用于文件中的多个模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9587310/

相关文章:

linux - 在 ansible 中从远程位置安装

java - JVM 在 eclipse 中运行,在 SIGSEGV 外部崩溃

linux - 从下面的 Shell 脚本中查找哪个文件有问题

linux - 模块: command not found

unix - Unix 中的 Roundup 函数

linux - 如何计算观看视频时的CPU负载(Linux上的Web服务器)

linux - 将特定列从二进制转换为十进制的 Shell 脚本

linux - 使用正则表达式对文件运行 awk

json - jq 以不同的方式对 KEY 和 VALUES 进行排序 - 如何以相同的顺序枚举它们?

我们可以修改 limits.h 吗?会不会有什么效果?