слияние многих файлов json в один

Вы используете записи в /etc/resolvconf/resolv.conf.d/ с именами head, base или tail (в зависимости от того, где в resolv.conf вы хотите видеть ваши записи). Из man resolvconf:

The dynamically generated resolver  configuration  file always  starts
with  the  contents of /etc/resolvconf/resolv.conf.d/head and ends with
the contents of /etc/resolvconf/resolv.conf.d/tail.  Between  head  and
tail  the  libc script inserts dynamic nameserver information compiled
from, first, information provided for  configured  interfaces;  second,
static  information  from /etc/resolvconf/resolv.conf.d/base.

Просто поместите ваши записи в base и запустите resolvconf -u.

0
27.03.2018, 08:21
4 ответа

jqраствор:

jq -s '{ attributes: map(.attributes[0]) }' file*.json
  • -s(--slurp)-вместо запуска фильтра для каждого объекта JSON во входных данных, считывайте весь входной поток в большой массив и запускайте фильтр только один раз.

Пример вывода:

{
  "attributes": [
    {
      "name": "Node",
      "value": "test"
    },
    {
      "name": "version",
      "value": "11.1"
    }
  ]
}
13
28.01.2020, 02:13

В отличие от решения Романа Перехреста , это позволит объединить целевое поле без переделки всего объекта:

jq -s '.[0].attributes = [.[].attributes | add] |.[0]' file*.json

Это возвращает весь первый файл json с .attributesиз всех остальных, объединенных вместе.

4
28.01.2020, 02:13

Использовать jsonmerge

>>> base = {
...         "foo": 1,
...         "bar": [ "one" ],
...      }

>>> head = {
...         "bar": [ "two" ],
...         "baz": "Hello, world!"
...     }

>>> from pprint import pprint

>>> from jsonmerge import merge
>>> result = merge(base, head)

>>> pprint(result, width=40)
{'bar': ['two'],
 'baz': 'Hello, world!',
 'foo': 1}
0
27.01.2021, 17:22

Еще один вариант jq, без чавканья, но с inputsиreduce

jq -n 'reduce inputs as $in ({}; reduce ($in|keys_unsorted)[] as $k (.;
        .[$k] += $in[$k]))'  file1 file2 
0
08.11.2021, 05:06

Теги

Похожие вопросы