How to search for packages that provides a virtual package?

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)?


Solution 1:

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

Solution 2:

Use aptitude. It's got a very rich search language. For searching "Provides" fields, use the ~P term:

$ aptitude search '~Px-terminal-emulator'
p   aterm                           - transitional dummy package for rxvt-unicod
p   aterm-ml                        - transitional dummy package for rxvt-unicod
p   eterm                           - Enlightened Terminal Emulator             
p   evilvte                         - lightweight terminal emulator based on VTE
p   gnome-terminal                  - GNOME terminal emulator application       
p   guake                           - Drop-down terminal for GNOME Desktop Envir
p   konsole                         - X terminal emulator                       
p   kterm                           - Multi-lingual terminal emulator for X     
p   lilyterm                        - Light and eazy-to-use terminal emulator fo
i A lxterminal                      - LXDE terminal emulator                    
p   mate-terminal                   - MATE terminal emulator application        
p   mlterm                          - MultiLingual TERMinal                     
p   mlterm-tiny                     - MultiLingual TERMinal, tiny version       
p   mrxvt                           - lightweight multi-tabbed X terminal emulat
p   mrxvt-cjk                       - lightweight multi-tabbed X terminal emulat
p   mrxvt-mini                      - lightweight multi-tabbed X terminal emulat
p   pterm                           - PuTTY terminal emulator                   
p   qterminal                       - Lightweight Qt terminal emulator          
p   roxterm-gtk2                    - Multi-tabbed GTK+/VTE terminal emulator - 
p   roxterm-gtk3                    - Multi-tabbed GTK+/VTE terminal emulator - 
i   rxvt                            - VT102 terminal emulator for the X Window S
p   rxvt-ml                         - multi-lingual VT102 terminal emulator for 
p   rxvt-unicode                    - RXVT-like terminal emulator with Unicode s
p   rxvt-unicode-256color           - multi-lingual terminal emulator with Unico
p   rxvt-unicode-lite               - RXVT-like terminal emulator with basic Uni
p   sakura                          - simple but powerful libvte-based terminal 
p   stterm                          - suckless tools simple terminal for windowe
p   terminal.app                    - Terminal Emulator for GNUstep             
i   terminator                      - multiple GNOME terminals in one window    
p   terminology                     - Enlightenment efl based terminal emulator 
p   termit                          - Simple terminal emulator based on vte libr
p   vala-terminal                   - Terminal emulator for mobile devices      
p   xfce4-terminal                  - Xfce terminal emulator                    
p   xiterm+thai                     - X terminal program with Thai languague sup
i   xterm                           - X terminal emulator                       
p   xvt                             - X terminal-emulator similar to xterm, but 

In theory, the ~s term should be able to limit by section (main, contrib, non-free), but that wasn't working properly for me for some reason.