我是Arch Linux用户,遇到JSON jq生成问题。
我的目标是运行pacman -Qi以便列出所有软件包和依赖项并将其转换为JSON。
但是我只想要名称,版本,描述和URL字段。
我尝试使用以下命令使用greps模式导出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,
答案 0 :(得分: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 <evangelos@foutrelis.com>
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 <jgc@archlinux.org>
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"
}