简体   繁体   English

如何将 txt 转换为 json?

[英]how to convert txt to json?

i try use jq convert was successful, but I didn't get the results I wanted.我尝试使用 jq convert 是成功的,但我没有得到我想要的结果。 I tried to get the results I wanted, but it's hard.我试图得到我想要的结果,但这很难。

jq -Rs '[ split("\n")[] | select(length > 0) | split(" ") |  {group:.[0], instnace: .[1], value: .[2]} ]' input.txt

original txt file.. There are IP addresses and values from cpu20 to 46 in total原始txt文件..从cpu20到46总共有IP地址和值

cpu20 10.0.20.1 12
cpu20 10.0.20.1 22
cpu20 10.0.20.1 13
cpu20 10.0.20.1 11
cpu20 10.0.20.1 14

....~cpu46 .....~cpu46

It's the result of the way I tried.这是我尝试的方式的结果。 output json output json

{
    "group": "cpu-46",
    "instnace": "10.0.46.94",
    "value": "10"
  },
  {
    "group": "cpu-46",
    "instnace": "10.0.46.95",
    "value": "10"
  },
  {
    "group": "cpu-46",
    "instnace": "10.0.46.96",
    "value": "11"
  },
  {
    "group": "cpu-46",
    "instnace": "10.0.46.97",
    "value": "8"
  },
  {
    "group": "cpu-46",
    "instnace": "10.0.46.98",
    "value": "11"
  },
  {
    "group": "cpu-46",
    "instnace": "10.0.46.99",
    "value": "11"
  },
  {
    "group": "cpu-46",
    "instnace": "10.0.46.100",
    "value": "8"
  }

What should I do to get the results as follows?我应该怎么做才能得到如下结果?

    {
  "CPU20": [
    { "instance": "10.0.20.1", "value": 12 },
    { "instance": "10.0.20.2", "value": 22 },
    { "instance": "10.0.20.3", "value": 13 },
    { "instance": "10.0.20.4", "value": 11 },
    { "instance": "10.0.20.5", "value": 14 }
  ],
  "CPU21": [
    { "instance": "10.0.21.1", "value": 15 },
    { "instance": "10.0.21.2", "value": 24 },
    { "instance": "10.0.21.3", "value": 21 },
    { "instance": "10.0.21.4", "value": 15 },
    { "instance": "10.0.21.5", "value": 16 }
  ],
  "CPU22": [
    { "instance": "10.0.22.1", "value": 12 },
    { "instance": "10.0.22.2", "value": 18 },
    { "instance": "10.0.22.3", "value": 19 },
    { "instance": "10.0.22.4", "value": 12 },
    { "instance": "10.0.22.5", "value": 13 }
  ],
  "CPU23": [
    { "instance": "10.0.20.1", "value": 17 },
    { "instance": "10.0.20.2", "value": 15 },
    { "instance": "10.0.20.3", "value": 21 },
    { "instance": "10.0.20.4", "value": 22 },
    { "instance": "10.0.20.5", "value": 25 }
  ]
}

Using group_by and map使用group_bymap

jq -Rn '
  [inputs | select(. != "") / " "]
  | group_by(.[0])
  | map({(.[0][0]): map({instance: .[1], value: .[2]})})
  | add
'

Demo演示

Using reduce使用reduce

jq -Rn '
  [inputs | select(. != "") / " "]
  | reduce .[] as [$cpu, $instance, $value] ({};
      .[$cpu] += [{$instance, $value}]
    )
'

Demo演示

A scripting language like ruby would work here as well:像 ruby 这样的脚本语言也可以在这里工作:

ruby -rjson -e '
  data = Hash.new {|h, k| h[k] = []}
  File.new(ARGV.shift).each {|line|
    cpu, ip, num = line.split
    data[cpu] << {instance: ip, value: num.to_i}
  }
  puts JSON.dump(data)
' input.txt
{"CPU20":[{"instance":"10.0.20.1","value":12},{"instance":"10.0.20.2","value":22},{"instance":"10.0.20.3","value":13},{"instance":"10.0.20.4","value":11},{"instance":"10.0.20.5","value":14}],"CPU21":[{"instance":"10.0.21.1","value":15},{"instance":"10.0.21.2","value":24},{"instance":"10.0.21.3","value":21},{"instance":"10.0.21.4","value":15},{"instance":"10.0.21.5","value":16}],"CPU22":[{"instance":"10.0.22.1","value":12},{"instance":"10.0.22.2","value":18},{"instance":"10.0.22.3","value":19},{"instance":"10.0.22.4","value":12},{"instance":"10.0.22.5","value":13}],"CPU23":[{"instance":"10.0.20.1","value":17},{"instance":"10.0.20.2","value":15},{"instance":"10.0.20.3","value":21},{"instance":"10.0.20.4","value":22},{"instance":"10.0.20.5","value":25}]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM