How to parse Debian package information

Use to drctl-tools to parse Debian package information using Debian control file format (the dctrl format).

Installation

Install drctl-tools and debtags packages.

$ sudo apt install drctl-tools debtools

Utilities

There are several utilities that perform the same task, but use different system databases.

Utility Database Database details
grep-dctrl dctrl-format files Single or multiple files provided as a parameter
grep-available DPKG available database File /var/lib/dpkg/available
grep-status DPKG status database File /var/lib/dpkg/status
grep-aptavail APT available database Command apt-cache dumpavail
grep-debtags Debtags package database Command debtags dumpavail

There are three additional utilities like sort-dctrl to sort data, tbl-ctrl to tabulate data and sync-available to sync DPKG available database with its APT counterpart.

Synchronize DPKG available database with APT counterpart

Use sync-available utility to perform synchronization.

$ sudo sync-available 
Merging available database in /tmp/apt-available.wTdjrp...done.
Replacing available packages info, using /tmp/apt-available.wTdjrp.
Information about 56805 packages was updated.

This is a very simple operation.

[...]
aptcache=/usr/bin/apt-cache
[...]
tf=`mktemp -t apt-available.XXXXXX` || exit 1
echo -n "Merging available database in $tf..."
$aptcache dumpavail >> $tf
echo "done."
dpkg --update-avail $tf
[...]

Usage

Count packages in specific repository.

$ grep-dctrl --count '' /var/lib/apt/lists/repo.proxysql.com_ProxySQL_proxysql-2.0.x_stretch_._Packages
5

Display packages defined in specific repository.

$ grep-dctrl --show-field Package,Version '' /var/lib/apt/lists/repo.proxysql.com_ProxySQL_proxysql-2.0.x_stretch_._Packages
Package: proxysql
Version: 2.0.4

Package: proxysql
Version: 2.0.2

Package: proxysql
Version: 2.0.3

Package: proxysql
Version: 2.0.1

Package: proxysql
Version: 2.0.5

Display packages defined in specific repository using a table.

$ grep-dctrl --show-field Package,Version '' /var/lib/apt/lists/repo.proxysql.com_ProxySQL_proxysql-2.0.x_stretch_._Packages | \
  sort-dctrl --key-spec Package,Version | \
  tbl-dctrl --column Package --column Version
+==========+=========+
| Package  | Version |
+----------+---------+
| proxysql | 2.0.1   |
| proxysql | 2.0.2   |
| proxysql | 2.0.3   |
| proxysql | 2.0.4   |
| proxysql | 2.0.5   |
+==========+=========+

Count installed packages.

$ grep-status --count --field Package ''
351

Display details of specific installed package.

$ grep-status --exact-match --field Package openssh-server --and --field Status "install ok installed"
Package: openssh-server
Status: install ok installed
Priority: optional
Section: net
Installed-Size: 1449
Maintainer: Debian OpenSSH Maintainers 
Architecture: amd64
Multi-Arch: foreign
Source: openssh
Version: 1:7.9p1-9
Replaces: openssh-client (<< 1:7.9p1-8), ssh, ssh-krb5
Provides: ssh-server
Depends: adduser (>= 3.9), dpkg (>= 1.9.0), libpam-modules (>= 0.72-9), libpam-runtime (>= 0.76-14), lsb-base (>= 4.1+Debian3), openssh-client (= 1:7.9p1-9), openssh-sftp-server, procps, ucf (>= 0.28), debconf (>= 0.5) | debconf-2.0, libaudit1 (>= 1:2.2.1), libc6 (>= 2.26), libcom-err2 (>= 1.43.9), libgssapi-krb5-2 (>= 1.17), libkrb5-3 (>= 1.13~alpha1+dfsg), libpam0g (>= 0.99.7.1), libselinux1 (>= 1.32), libssl1.1 (>= 1.1.1), libsystemd0, libwrap0 (>= 7.6-4~), zlib1g (>= 1:1.1.4)
Recommends: default-logind | logind | libpam-systemd, ncurses-term, xauth
Suggests: molly-guard, monkeysphere, rssh, ssh-askpass, ufw
Conflicts: sftp, ssh-socks, ssh2
Conffiles:
 /etc/default/ssh 500e3cf069fe9a7b9936108eb9d9c035
 /etc/init.d/ssh 0adcea4e477bba5422f1ced4ec15ce64
 /etc/pam.d/sshd 8b4c7a12b031424b2a9946881da59812
 /etc/ssh/moduli 3f02069ef69e6636b879ec3c0706a664
 /etc/ufw/applications.d/openssh-server 486b78d54b93cc9fdc950c1d52ff479e
Description: secure shell (SSH) server, for secure access from remote machines
 This is the portable version of OpenSSH, a free implementation of
 the Secure Shell protocol as specified by the IETF secsh working
 group.
 .
 Ssh (Secure Shell) is a program for logging into a remote machine
 and for executing commands on a remote machine.
 It provides secure encrypted communications between two untrusted
 hosts over an insecure network. X11 connections and arbitrary TCP/IP
 ports can also be forwarded over the secure channel.
 It can be used to provide applications with a secure communication
 channel.
 .
 This package provides the sshd server.
 .
 In some countries it may be illegal to use any encryption at all
 without a special permit.
 .
 sshd replaces the insecure rshd program, which is obsolete for most
 purposes.
Homepage: http://www.openssh.com/

Check if specific version is installed.

$ grep-status --quiet --exact-match --field Package openssh-server --and --field Version --ge "1:7.9" && echo PASSED || echo FAILED
PASSED
$ grep-status --quiet --exact-match --field Package openssh-server --and --field Version --ge "1:7.11" && echo PASSED || echo FAILED
FAILED

Search packages by tags.

$ grep-available  \( \( --field Tag "interface::commandline" --and --field Tag "uitoolkit::ncurses" \) --and --field Tag "role::program" --and --field Tag "use::organizing" \) --and --not --field Section "x11" --show-field Package,Description | tbl-dctrl
+==========+==========================================================+
| Package  | Description                                              |
+----------+----------------------------------------------------------+
| calcurse | text-based calendar and todo manager                     |
| grass    | Geographic Resources Analysis Support System (GRASS GIS) |
| mc       | Midnight Commander - a powerful file manager             |
| wyrd     | text-based calendar application                          |
| xstow    | Extended replacement of GNU Stow                         |
+==========+==========================================================+

Display the way how the current command line is parsed.

$ grep-available  \( \( --field Tag "interface::commandline" --and --field Tag "uitoolkit::ncurses" \) --and --field Tag "role::program" --and  --field Tag "use::organizing" \) --and --not --field Section "x11" --debug-optparse
tokens: ( ( -F[Tag] string[interface::commandline] -a -F[Tag] string[uitoolkit::ncurses] ) -a -F[Tag] string[role::program] -a -F[Tag] string[use::organizing] ) -a ! -F[Section] string[x11]
AND
 AND
  AND
   AND
    ATOM     field_name = Tag
     mode = 0
     ignore_case = 0
     whole_pkg = 0
     pat = interface::commandline
    ATOM     field_name = Tag
     mode = 0
     ignore_case = 0
     whole_pkg = 0
     pat = uitoolkit::ncurses
   ATOM    field_name = Tag
    mode = 0
    ignore_case = 0
    whole_pkg = 0
    pat = role::program
  ATOM   field_name = Tag
   mode = 0
   ignore_case = 0
   whole_pkg = 0
   pat = use::organizing
 NOT
  ATOM   field_name = Section
   mode = 0
   ignore_case = 0
   whole_pkg = 0
   pat = x11
num_fnames = 0

Display top 10 maintainers.

$ grep-available --field Maintainer '' --show-field Maintainer --no-field-names | \
  sort | \
  uniq --count | \
  sort --human-numeric-sort --reverse | \
  head | \
  sed "s/@/ at /"
   3616 Debian Perl Group <pkg-perl-maintainers at lists.alioth.debian.org>
   2954 Debian Haskell Group <pkg-haskell-maintainers at lists.alioth.debian.org>
   2188 Debian Python Modules Team <python-modules-team at lists.alioth.debian.org>
   1800 Debian Science Maintainers <debian-science-maintainers at lists.alioth.debian.org>
   1774 Debian Java Maintainers <pkg-java-maintainers at lists.alioth.debian.org>
   1664 Debian GCC Maintainers <debian-gcc at lists.debian.org>
   1646 Debian Javascript Maintainers <pkg-javascript-devel at lists.alioth.debian.org>
   1335 Debian QA Group <packages at qa.debian.org>
   1233 Debian Med Packaging Team <debian-med-packaging at lists.alioth.debian.org>
   1165 Debian Ruby Extras Maintainers <pkg-ruby-extras-maintainers at lists.alioth.debian.org>

Display top 10 installed packages by size.

$ grep-status --show-field Installed-Size,Package --no-field-names --field Status "install ok installed" | \
  paste --serial --delimiters "  \n" | \
  sort --human-numeric-sort --reverse | \
  awk '{MB=$1/1024; printf("%.1f Mb %s\n",MB,$NF)}' | \
  head
257.3 Mb linux-image-4.19.0-4-amd64
30.9 Mb libicu63
25.6 Mb libperl5.28
19.0 Mb iso-codes
18.5 Mb perl-modules-5.28
15.8 Mb grub-common
15.7 Mb locales
15.4 Mb coreutils
13.2 Mb systemd
12.0 Mb libc6

Display top 10 available packages by size.

$ grep-available --show-field Installed-Size,Package --no-field-names --field Package '' | \
  paste --serial --delimiters "  \n" | \
  sort --human-numeric-sort --reverse | \
  awk '{MB=$1/1024; printf("%.1f Mb %s\n",MB,$NF)}' | \
  head
7216.6 Mb unidic-mecab
4920.2 Mb linux-image-4.19.0-5-rt-amd64-dbg
4919.7 Mb linux-image-4.19.0-5-amd64-dbg
4742.6 Mb kicad-packages3d
1996.3 Mb 0ad-data
1965.3 Mb piglit
1688.6 Mb flightgear-data-base
1468.1 Mb linux-image-4.19.0-5-cloud-amd64-dbg
1235.4 Mb acl2-books
1076.0 Mb texlive-fonts-extra

Additional information

There is a lot more information you can process, so check out the grep-dctrl manual page.