Trying to use dpkg and egrep commands to list packages whose names starts with q. Already tried:

dpkg -l | egrep -l q
dpkg -l | egrep -l ^q
dpkg -l | egrep q
dpkg -l | grep q

What is going wrong?

  • What is the result and how is it different from what you expect? – RalfFriedl Oct 15 at 22:26

You don't really need grep (or egrep) at all here: the dpkg -l command accepts a pattern:

-l, --list package-name-pattern...
    List packages matching given pattern.

Note that package-name-pattern is a glob pattern not a regular expression. So

dpkg -l 'q*'

If you want the output in more configurable format (for example, only the binay package name) then you can use dpkg-query instead ex.

dpkg-query -Wf '${binary:Package}\n' 'q*'

Use --get-selections instead of -l option:

dpkg --get-selections |grep ^q

Or using awk to change the column order:

dpkg -l |awk '{print $2 , $3 "\t\t" $1}' | grep ^q

The regular expressions for egrep do not match the expected output of dpkg -l. If you wish to keep the same output format as dpkg -l, which includes the sate of the package, the version and a description, then the regular expression needs to be changed to match the expected format: three characters at the start of the line (describing the state of the package on the system) and a space before the package name.

In the expression ^... [qQ], ^ indicates to match from the start of a line, ... says to match any three characters followed by a space, then q matches packages that start with either an upper or lower case "q" (packages should only be lowercase on Ubuntu but there may be custom packages that do not follow these rules).

$ dpkg -l | egrep '^... [qQ]'
ii  qdbus                                         4:4.8.7+dfsg-7ubuntu1                        amd64        Qt 4 D-Bus tool
ii  qemu-block-extra:amd64                        1:2.11+dfsg-1ubuntu7.19                      amd64        extra block backend modules for qemu-system and qemu-utils

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.