Back in 2010, there was a thread about using regular expression to extract version number from package atom, it’s not easy, even there is a documentation about the Version Specification.
I accidentally stumbled on versionsort command, then realized that it comes from eix, a command and package I use a lot. Its documentation is within eix(1):
versionsort [-n|-p|-v|-r] [package-]version ...
versionsort is a helper tool for scripts which serves two purposes. It cuts the version strings from its arguments or interprets the arguments themselves as version strings (depending on some heuristics). Then it outputs the version strings in a sorted order, according to the portage rules of version sorting.
It can parse and output the version numbers like:
$ versionsort cisco-vpnclient-3des/cisco-vpnclient-3des-4.8.00.0490-r1
4.8.00.0490-r1
$ versionsort font-adobe-utopia-100dpi/font-adobe-utopia-100dpi-1.0.2
1.0.2
$ versionsort scratchbox-toolchain-cs2009q1-203sb1-1.0.13
1.0.13
You can feed it more than one packages at a time. No need to use complicated regular expression like one from the thread if you just want a quick way to get the version number for your script:
/^
([<>]?=?) # Version specifier
(([^\/]+)\/)? # Category
(?U) # Makes rest of pattern ungreedy
(\S+) # Package name
(-(
\d+(\.\d+)*[a-z]? # Version (number part)
(_(alpha|beta|pre|rc|p)\d*)* # Version (release/patchlevel part)
(-r\d+)? # Version (revision part)
)?)
$/
Just give it to versionsort and it answers you the right version numbers, and they will be sorted perfectly from oldest to newest:
$ versionsort gcc-4.4 4.4_alpha0 sys-devel/gcc-4.05 4.5
4.05
4.4_alpha0
4.4
4.5
There are more to it, please read eix(1) for more.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.