从txt文件中提取多个JSON对象

问题描述 投票:0回答:1

我是Linux大用户,并且遇到JSON jq生成问题。

我的目标是运行pacman -Qi以便列出所有软件包和依赖项并将其转换为JSON。

但是我只想要名称,版本,描述和URL字段。

我已经尝试通过以下命令使用抓拍模式导出pacman -Qi的输出:

pacman -Qi | grep "Name\|Version\|Description\|URL" > /home/packages.txt

[我尝试使用以下命令用jq生成JSON文件后:

jq -R -n -c '[inputs|split(",")|{(.[0]):.[1]}] | add' packages.txt > packages-raw.json

此后,我没有格式化JSON输出,但它似乎是JSON输出。

我使用...格式化文件

jq . packages-raw.json

JSON似乎已格式化,但是当我使用VScode运行时,只有一个对象。

您能帮我有关格式吗?

先谢谢您,

最诚挚的问候

我想要一个输出为:

{
  "Name"            : "acl",
  "Version"         : "2.2.53-1",
  "Description"     : "Access control list utilities": " libraries and headers",
  "URL"             : "http://savannah.nongnu.org/projects/acl"
}

{ "Name"            : "archlinux-keyring",
  "Version"         : "20190827-1",
  "Description"     : "Arch Linux PGP keyring",
  "URL"             : "https://projects.archlinux.org/archlinux-keyring.git/"
} 

目前,我的输出为:

{ "Name : acl": null, "Version : 2.2.53-1": null, "Description : Access control list utilities": " libraries and headers", "URL : http://savannah.nongnu.org/projects/acl": null, "Name : archlinux-keyring": null, "Version : 20190827-1": null, "Description : Arch Linux PGP keyring": null, "URL : https://projects.archlinux.org/archlinux-keyring.git/": null, "Name : argon2": null, "Version : 20190702-1": null, "Description : A password-hashing function (reference C implementation)": null, "URL : https://github.com/P-H-C/phc-winner-argon2": null,
json jq
1个回答
0
投票

我没有Arch系统,但从外观上看

我假设pacman -Qi的输出如下所示>>

Name            : vi
Version         : 1:070224-2
Description     : The original ex/vi text editor
Architecture    : x86_64
URL             : http://ex-vi.sourceforge.net/
Licenses        : custom:ex
Groups          : base
Provides        : None
Depends On      : ncurses
Optional Deps   : s-nail: used by the preserve command for notification [installed]
Required By     : None
Optional For    : None
Conflicts With  : None
Replaces        : None
Installed Size  : 290.00 KiB
Packager        : Evangelos Foutras <[email protected]>
Build Date      : Sun 06 Sep 2015 09:34:15 PM CEST
Install Date    : Mon 03 Oct 2016 07:18:13 PM CEST
Install Reason  : Explicitly installed
Install Script  : No
Validated By    : Signature

Name            : dbus
Version         : 1.4.1-1
URL             : http://www.freedesktop.org/Software/dbus
Licenses        : GPL custom
Groups          : None
Provides        : None
Depends On      : dbus-core>=1.4.1 libx11
Optional Deps   : None
Required By     : avahi perl-net-dbus qt system-tools-backends
Conflicts With  : None
Replaces        : None
Installed Size  : 112.00 K
Packager        : Jan de Groot <[email protected]>
Architecture    : i686
Build Date      : Wed Dec 22 14:39:28 2010
Install Date    : Sun Jan 2 16:05:50 2011
Install Reason  : Installed as a dependency for another package
Install Script  : No
Description     : Freedesktop.org message bus system

这里是一个jq脚本,它将这些值转换为键值对象流并根据Name属性的值将它们收集到聚合对象中]

def kvpairs:                                                    # e.g. (note spaces)
    inputs                                                      # "Name    : vi"
  | capture("^(?<key>[^:]+):(?<value>.+)$")                     # { "key": "Name   ", "value": " vi" }
  | (.key, .value) |= gsub("^\\s+|\\s+$";"")                    # { "key": "Name", "value": "vi" }
  ;

  reduce kvpairs as $kv (
      { result:{}, cur:null }                                   # initial state
    ; if $kv.key == "Name" then .cur = $kv.value else . end     # track current object
    | .result[.cur][$kv.key] = $kv.value                        # apply update to current object
  )
| .result[]                                                     # generate result
| { Name, Version, Description, URL }                           # with only these members

假设test.jq中为上述内容并在test.pac中为pacman输出的示例执行>]

$ jq -MRn -f test.jq test.pac
{
  "Name": "vi",
  "Version": "1:070224-2",
  "Description": "The original ex/vi text editor",
  "URL": "http://ex-vi.sourceforge.net/"
}
{
  "Name": "dbus",
  "Version": "1.4.1-1",
  "Description": "Freedesktop.org message bus system",
  "URL": "http://www.freedesktop.org/Software/dbus"
}
© www.soinside.com 2019 - 2024. All rights reserved.