0

running dpkg --list gives me this output

ii  ucf                                           3.0036                      all                         Update Configuration File(s): preserve user changes to config files
ii  udev                                          229-4ubuntu11               amd64                       /dev/ and hotplug management daemon
ii  ufw                                           0.35-0ubuntu2               all                         program for managing a Netfilter firewall
ii  uidmap                                        1:4.2-3.1ubuntu5            amd64                       programs to help use subuids
ii  unattended-upgrades                           0.90                        all                         automatic installation of security upgrades
ii  unzip                                         6.0-20ubuntu1               amd64                       De-archiver for .zip files

Now for ex. let's take third row ufw

In third column where version numbers re written, I don't want it to return 0.35-0ubuntu2. It should return only 0.35-0 How can i do this?

Moreover what does this extra ubuntu11 denotes/means??

  • 1
    As for the "moreover": http://askubuntu.com/questions/620533/how-does-ubuntu-name-packages – muru Jan 10 '17 at 12:07

1 Answers1

0

This is a job for dpkg-query with manual formatting for desired output, on my system:

% dpkg-query -Wf '${Version}\n' ufw  
0.34~rc-0ubuntu2

The dpkg-query specific variable Version expands to the version of the mentioned package, ufw in this case.

Now to get the portion before ubuntu2, you can set the width too after Version, separated by ;:

% dpkg-query -Wf '${Version;9}\n' ufw
0.34~rc-0

but this is not reliable as the length can obviously vary for different possible versions; so you can leverage a bit of text-processing, here grep-ping (you can obviously use your tool of choice):

% dpkg-query -Wf '${Version}\n' ufw | grep -Po '.*?(?=ubuntu[^[:alpha:]]*$)'
0.34~rc-0

For the sake of completeness, if you insist on using dpkg -l, use a bit of awk:

%  dpkg -l | awk '$2=="ufw" {sub("ubuntu[^[:alpha:]]*$", "", $3); print $3}'
0.34~rc-0
heemayl
  • 91,753