I would like to extract from a package name only the version of the package. Assuming I have a variable var that contains a package name (E.g.: var="nfs-utils-1.2.6-6.fc17.i686.rpm" ). The string extracted would be 1.2.6-6. The method used to parse can be anything (regex, awk, cut).

Edit: In the example above I would actually like to extract 1.2.6

  • 4
    rpm --queryformat ... would be a far superior way rather than reinventing RPM version string parsing.
    – thrig
    Jan 20, 2017 at 19:50
  • 4
    in your example, "1.2.6" is the version, while "6.fc17" is the release; are you actually trying to get the (rpm) %VERSION, or do you actually want to include the first piece of the %RELEASE? see: rpm -qa --queryformat="%{NAME} %{VERSION} %{RELEASE}\n"
    – Jeff Schaller
    Jan 20, 2017 at 21:38
  • I was not aware that the 6 after the hifen was part of the release. Thanks for clarifying this. All I care for is the version. Jan 21, 2017 at 0:17

3 Answers 3


This is not very portable, but for this specific case, this grep works:

echo $var | egrep -o '[0-9].*-[0-9]'
  • By not very portable you mean between different linux distros? I am using it exclusively on FC 17 and FC 22 Jan 20, 2017 at 19:31
  • egrep can be installed in every linux distros (except the no-gnu distros, like android). I think he is talking about other unix systems ;-) Jan 20, 2017 at 19:32
  • No, I mean that it will not work on a different package name; for instance if the version is 1.2.33 this grep will not work.
    – anon
    Jan 20, 2017 at 19:33

As thrig said, if you're extracting the values on the same system where those packages are installed, you can ask RPM for the version directly:

ver=$(rpm --queryformat="%{VERSION}" -q nfs-utils)
  • Thats nice! I liked it. Jan 22, 2017 at 17:43
  • I am trying to compare the version of a package saved in a folder with the version of the packages currently installed. If they differ, I attempt to install the package saved in the folder. Jan 23, 2017 at 17:48
  • then grab the other version with ver2=$(rpm --queryformat="%{VERSION}" -qp /path/to/saved/folder/package.rpm
    – Jeff Schaller
    Jan 23, 2017 at 18:43

The last 2 '-' characters surround the version.

So one can write a *RE that is anchored at the end of the string with a '$'


$ echo nfs-utils-1.2.6-6.fc17.i686.rpm | sed -e 's,.*-\([^-]*\)-[^-]*$,\1,'


  • $ echo nfs-utils-1.2.6-6.fc17.i686.rpm | sed -e 's,.*-([^-]*-[^-]*).i686.rpm$,\1,' 1.2.6-6.fc17 Feb 9, 2017 at 19:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .