Ubuntu – How to search for packages that provides a virtual package

package-management

How to search for packages that provides a virtual package?

For example, I want to search for packages that provides "x-terminal-emulator" in the "main" repository of Ubuntu 12.04. One way to do this is to parse the package index:

curl http://archive.ubuntu.com/ubuntu/dists/precise/main/binary-amd64/Packages.gz | zcat | grep -B12 '^Provides: x-terminal-emulator' | grep ^Package:

which gives me the following results:

Package: gnome-terminal
Package: konsole
Package: xterm

Is there a better (and cleaner) way to do this?
Can it be done with any of the official tools (apt-get/apt-cache/etc)?

Best Answer

Assuming you know x-terminal-emulator is a virtual package, you may try

# absolutely blunt method
apt-provides() {
  apt-cache show '.*' |
    sed -n '/^Package: \(.*\)$/ {s//\1/;h}; /^Provides:.*'"$1"'/ {x;p}' | sort | uniq
}

# better method
apt-provides() {
  apt-cache show $(apt-cache search "$1" | awk '{ print $1 }' | tr '\n' ' ') |
    sed -n '/^Package: \(.*\)$/ {s//\1/;h}; /^Provides:.*'"$1"'/ {x;p}'
}

For example,

$ apt-provides x-terminal-emulator

gnome-terminal
xterm
aterm
aterm-ml
eterm
evilvte
guake
konsole
kterm
lxterminal
mlterm
mlterm-tiny
mrxvt
mrxvt-cjk
mrxvt-mini
pterm
roxterm-gtk2
roxterm-gtk3
rxvt
rxvt-beta
rxvt-ml
rxvt-unicode
rxvt-unicode-256color
rxvt-unicode-lite
sakura
terminal.app
terminator
termit
vala-terminal
xfce4-terminal
xiterm+thai
xvt

edit: included a blunt method for error-checking and made sed expression nicer

Related Question