Debian, Ubuntu and other derived GNU/Linux distributions have their package system based on the .deb
packages and tools like apt
(or formerly aptitude
and apt-get
).
This is a command line tool for installing packages and also for searching, showing metadata and other related tasks.
We can show a metadata for a package this way:
$ apt show emacs
Package: emacs
Version: 47.0
Priority: optional
Section: editors
Source: emacs-defaults
Installed-Size: 8 192 B
Supported: 5y
Download-Size: 1 748 B
Description: GNU Emacs editor (metapackage)
GNU Emacs is the extensible self-documenting text editor.
This is a metapackage that will always depend on the latest
recommended Emacs release.
…
This is some kind of ordinary human-readable listing.
But, it seems familiar, like we have met somewhere before… Yes, it is almost the same format as recfile from GNU Recutils!
There are field names, colons and values. Each pair on a separate line.
And there is also a convention for multi-line text – just with a little difference: indentation instead of +
.
But we can tune it with just a little sed
transformation
and then read it like a recfile:
apt show emacs25* \
| sed 's/^ /+/g' \
| relpipe-in-recfile \
| relpipe-tr-cut --relation '.*' --attribute 'Package|Version|Section|Installed-Size|Homepage|Supported' \
| relpipe-out-tabular
And print results in a pretty table:
recfile: ╭─────────────────────────┬──────────────────┬────────────────────┬─────────────────────────┬────────────────────────────────────┬────────────────────╮ │ Package (string) │ Version (string) │ Section (string) │ Installed-Size (string) │ Homepage (string) │ Supported (string) │ ├─────────────────────────┼──────────────────┼────────────────────┼─────────────────────────┼────────────────────────────────────┼────────────────────┤ │ emacs25 │ 25.2+1-6 │ editors │ 19,7 MB │ http://www.gnu.org/software/emacs/ │ 5y │ │ emacs25-nox │ 25.2+1-6 │ editors │ 17,9 MB │ http://www.gnu.org/software/emacs/ │ 5y │ │ emacs25-bin-common │ 25.2+1-6 │ editors │ 473 kB │ http://www.gnu.org/software/emacs/ │ 5y │ │ emacs25-common-non-dfsg │ 25.2+1-1 │ multiverse/editors │ 4 685 kB │ │ │ │ emacs25-common │ 25.2+1-6 │ editors │ 66,9 MB │ http://www.gnu.org/software/emacs/ │ 5y │ │ emacs25-el │ 25.2+1-6 │ editors │ 16,3 MB │ http://www.gnu.org/software/emacs/ │ 5y │ │ emacs25-dbg │ 25.2+1-6 │ debug │ 5 604 kB │ http://www.gnu.org/software/emacs/ │ 5y │ │ emacs25-nox-dbg │ 25.2+1-6 │ debug │ 3 996 kB │ http://www.gnu.org/software/emacs/ │ 5y │ ╰─────────────────────────┴──────────────────┴────────────────────┴─────────────────────────┴────────────────────────────────────┴────────────────────╯ Record count: 8
We can use also GNU Recutils, but we have to rename some fields like Installed-Size
because the -
hyphen does not seem to be edible.
This can be fixed this way:
apt show emacs* \
| sed -E \
-e 's/^([^-]+)-([^-]+)-([^:]+)/\1_\2_\3/g' \
-e 's/^([^-]+)-([^:]+)/\1_\2/g' \
-e 's/^ /+/g' \
| recsel -p Package,Version,Homepage
However, it is still quite dirty, because apt
complains about:
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
So it would be better to introduce some machine-readable output in the apt
tool.
But until that: Happy hacking!
For some ad-hoc operations it is usable and we can e.g. convert the results to a CSV or a LibreOffice Calc file (ODS).
apt show emacs25* \
| sed 's/^ /+/g' \
| relpipe-in-recfile \
| relpipe-tr-cut --relation '.*' --attribute 'Package|Version|Section|Installed-Size|Homepage|Supported' \
| relpipe-out-ods \
> apt-packages.fods
The machine-readable format should also carry the sizes in bytes instead of human-friendly units.
Good news are that the Guix SD package system already prints results in regular recfile format.
Relational pipes, open standard and free software © 2018-2022 GlobalCode