比较两个JSON文件并选择所有具有更改属性的条目。

huangapple go评论72阅读模式
英文:

Compare two JSON files and select all entries with a changed attribute

问题

我有两个包含对等统计信息的JSON文件:

File1.json:

{
  "transferIN": 111111,
  "transferOUT": 111234,
  "IPs": [
    "10.1.1.1/32"
  ]
}
{
  "transferIN": 111222,
  "transferOUT": 222234,
  "IPs": [
    "10.1.1.2/32"
  ]
}
{
  "transferIN": 111333,
  "transferOUT": 333556,
  "IPs": [
    "10.1.1.3/32"
  ]
}
{
  "transferIN": 111444,
  "transferOUT": 444875,
  "IPs": [
    "10.1.1.4/32"
  ]
}

File2.json:

{
  "transferIN": 111111,
  "transferOUT": 111234,
  "IPs": [
    "10.1.1.1/32"
  ]
}
{
  "transferIN": 111345,
  "transferOUT": 222233,
  "IPs": [
    "10.1.1.2/32"
  ]
}
{
  "transferIN": 111333,
  "transferOUT": 333990,
  "IPs": [
    "10.1.1.3/32"
  ]
}
{
  "transferIN": 111446,
  "transferOUT": 4448456,
  "IPs": [
    "10.1.1.4/32"
  ]
}

我想要比较它们,并只获取具有更改的 "transferIN" 键的IP列表。

因此,输出应如下所示:

10.1.1.2/32
10.1.1.4/32

我将在bash脚本中使用这个。所以我开始研究jq。

我所做的:

jq -n --slurpfile file1 File1.json --slurpfile file2 File2.json '$file2 - $file1' | jq .[] | jq -r .IPs[]

但这只显示具有任何更改的对等体统计信息(transferOUT 也可能更改)。

10.1.1.2/32
10.1.1.3/32
10.1.1.4/32

我还尝试过使用INDEX和map_values,但没有得到想要的结果。

英文:

I have two JSON files with peers statistics:

File1.json:

{
  "transferIN": 111111,
  "transferOUT": 111234,
  "IPs": [
    "10.1.1.1/32"
  ]
}
{
  "transferIN": 111222,
  "transferOUT": 222234,
  "IPs": [
    "10.1.1.2/32"
  ]
}
{
  "transferIN": 111333,
  "transferOUT": 333556,
  "IPs": [
    "10.1.1.3/32"
  ]
}
{
  "transferIN": 111444,
  "transferOUT": 444875,
  "IPs": [
    "10.1.1.4/32"
  ]
}

File2.json:

{
  "transferIN": 111111,
  "transferOUT": 111234,
  "IPs": [
    "10.1.1.1/32"
  ]
}
{
  "transferIN": 111345,
  "transferOUT": 222233,
  "IPs": [
    "10.1.1.2/32"
  ]
}
{
  "transferIN": 111333,
  "transferOUT": 333990,
  "IPs": [
    "10.1.1.3/32"
  ]
}
{
  "transferIN": 111446,
  "transferOUT": 4448456,
  "IPs": [
    "10.1.1.4/32"
  ]
}

I want to compare them and get a list of IPs only which has a changed "transferIN" key.

So output should be like this:
10.1.1.2/32
10.1.1.4/32

I will use this in the bash script. So I started to look into jq.

What I did:

jq -n --slurpfile file1 File1.json --slurpfile file2 File2.json '$file2 - $file1' | jq .[] | jq -r .IPs[]

But this only show peers that have any changed stats (transferOUT can change also)

10.1.1.2/32
10.1.1.3/32
10.1.1.4/32

Also looked to INDEX and map_values but with no result.

答案1

得分: 3

假设您想比较各自流中匹配位置的项目,您可以仅读取一个文件,然后使用input作为流连续读取另一个文件的项目,同时使用$file1[]迭代已读取的项目:

<File2.json jq --slurpfile file1 File1.json -rn \
  '$file1[] | select(.transferIN != input.transferIN).IPs[]'
10.1.1.2/32
10.1.1.4/32
英文:

Assuming you want to compare items of matching positions within their respective streams, you could slurp just one file, and successively read in the other file's items as a stream using input while iterating over the slurped ones using $file1[]:

<File2.json jq --slurpfile file1 File1.json -rn \
  '$file1[] | select(.transferIN != input.transferIN).IPs[]'
10.1.1.2/32
10.1.1.4/32

huangapple
  • 本文由 发表于 2023年7月17日 18:16:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定