Print list of installed Alpine packages with version in an easily parse-able format

I am trying to get a list of installed packages with version in an easily processable format for an automated tool to parse.

I can get this output:

/ # apk list -i
WARNING: Ignoring APKINDEX.00740ba1.tar.gz: No such file or directory
WARNING: Ignoring APKINDEX.d8b2a6f4.tar.gz: No such file or directory
musl-1.1.22-r2 x86_64 {musl} (MIT) [installed]
zlib-1.2.11-r1 x86_64 {zlib} (zlib) [installed]
apk-tools-2.10.4-r2 x86_64 {apk-tools} (GPL2) [installed]
musl-utils-1.1.22-r2 x86_64 {musl} (MIT BSD GPL2+) [installed]
libssl1.1-1.1.1c-r0 x86_64 {openssl} (OpenSSL) [installed]
alpine-baselayout-3.1.2-r0 x86_64 {alpine-baselayout} (GPL-2.0-only) [installed]
alpine-keys-2.1-r2 x86_64 {alpine-keys} (MIT) [installed]
busybox-1.30.1-r2 x86_64 {busybox} (GPL-2.0) [installed]
scanelf-1.2.3-r0 x86_64 {pax-utils} (GPL-2.0) [installed]
libc-utils-0.7.1-r0 x86_64 {libc-dev} (BSD) [installed]
libtls-standalone-2.9.1-r0 x86_64 {libtls-standalone} (ISC) [installed]
ssl_client-1.30.1-r2 x86_64 {busybox} (GPL-2.0) [installed]
ca-certificates-cacert-20190108-r0 x86_64 {ca-certificates} (MPL-2.0 GPL-2.0-or-later) [installed]
libcrypto1.1-1.1.1c-r0 x86_64 {openssl} (OpenSSL) [installed]

However, packages names can contain hyphens and versions are seperated by hyphens, this makes it impossible to reliably parse this result.

Is there any way to get this info in an easier to parse format? JSON would be amazing but a simple tab separated table would be fine.

Thanks!

1 Answer

However, packages names can contain hyphens and versions are seperated by hyphens, this makes it impossible to reliably parse this result.

Versions themselves cannot contain hyphens – there is always exactly one group for the pkgver field and always exactly one for the pkgrel. So it's actually very easy to parse by separating the last two dash-separated groups; the package name is what remains.

Most programming languages can do so using either a regex, or functions like .rsplit() / .rindex().

For example, this PCRE regex will match all fields:

/(.+)-([^-]+)-r([^-]+) (\S+) \{(\S+)\} \((.+?)\)/

To generate something like JSON output with that, use:

apk list -i | perl -MJSON -E 'print encode_json([map { @foo{qw(pkgname pkgver pkgrel arch group license)} = /(.+)-([^-]+)-r([^-]+) (\S+) \{(\S+)\} \((.+?)\)/; \%foo } <>])'

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like