PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > How to grab version number?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

How to grab version number?

Réponse
 
LinkBack Outils de la discussion
Vieux 05/12/2006, 20h07   #1
Charles A. Landemaine
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to grab version number?

I have a string that reports the whole name, version and description of
an application:

lighttpd-1.4.10 (ssl) - a light and fast webserver

How could I grab only the version, which is only the (first) group of
numbers separated by dots?
Thanks in advance.

  Réponse avec citation
Vieux 05/12/2006, 20h19   #2
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to grab version number?

On 5 Dec 2006 12:07:29 -0800, Charles A. Landemaine
<landemaine@gmail.com> wrote:
> I have a string that reports the whole name, version and description of
> an application:
>
> lighttpd-1.4.10 (ssl) - a light and fast webserver
>
> How could I grab only the version, which is only the (first) group of
> numbers separated by dots?
> Thanks in advance.
>

sed 's/[^-]*-\([^ ]*\).*/\1/


--
"Marriage is low down, but you spend the rest of your life paying for it."
-- Baskins
  Réponse avec citation
Vieux 05/12/2006, 20h33   #3
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to grab version number?

2006-12-5, 12:07(-08), Charles A. Landemaine:
> I have a string that reports the whole name, version and description of
> an application:
>
> lighttpd-1.4.10 (ssl) - a light and fast webserver
>
> How could I grab only the version, which is only the (first) group of
> numbers separated by dots?
> Thanks in advance.


NL='
'
string='lighttpd-1.4.10 (ssl) - a light and fast webserver'
printf '%s\n' "$string" |
sed "
s/\([0-9]\{1,\}\(\.[0-9]\{1,\}\)\{1,\}\).*/\\$NL\1/
s/.*\n//"

Or:

perl -le 'print $ARGV[0] =~ /(\d+(?:\.\d+)+)/' -- "$string"

--
Stéphane
  Réponse avec citation
Vieux 05/12/2006, 20h36   #4
Charles A. Landemaine
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to grab version number?

Bill Marcum wrote:
> sed 's/[^-]*-\([^ ]*\).*/\1/


Incredible! It works! Thanks, Bill

  Réponse avec citation
Vieux 05/12/2006, 20h37   #5
Charles A. Landemaine
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to grab version number?

Thank you guys, you're geniuses!

  Réponse avec citation
Vieux 05/12/2006, 22h26   #6
* Tong *
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to grab version number?

On Tue, 05 Dec 2006 20:33:51 +0000, Stephane CHAZELAS wrote:

>> lighttpd-1.4.10 (ssl) - a light and fast webserver
>>
>> How could I grab only the version, which is only the (first) group of
>> numbers separated by dots?


be careful what you ask for!

> NL='
> '
> string='lighttpd-1.4.10 (ssl) - a light and fast webserver'
> printf '%s\n' "$string" |
> sed "
> s/\([0-9]\{1,\}\(\.[0-9]\{1,\}\)\{1,\}\).*/\\$NL\1/
> s/.*\n//"
>
> Or:
>
> perl -le 'print $ARGV[0] =~ /(\d+(?:\.\d+)+)/' -- "$string"


Stephane's answer is exactly what you asked for -- the (first) group of
numbers separated by dots.

IMHO, when talking about version numbers, what you asked for might not be
(totally) correct. There might be underlines, or even characters. E.g.,
here is a (very limited) list of all forms a Debian version number may
looks like:

foomatic-filters_3.0.2-20061031-1
gimp_2.2.13-1
gimp-data_2.2.13-1
gs-esp_8.15.3.dfsg.1-1
libcupsys2_1.2.5-1
libgimp2.0_2.2.13-1

I know yours may not, but just be cautious.

--
Tong (remove underscore(s) to reply)
http://xpt.sourceforge.net/


--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 05/12/2006, 23h13   #7
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to grab version number?

Charles A. Landemaine wrote:

> I have a string that reports the whole name, version and description of
> an application:
>
> lighttpd-1.4.10 (ssl) - a light and fast webserver
>
> How could I grab only the version, which is only the (first) group of
> numbers separated by dots?
> Thanks in advance.
>


man cut

Ed.
  Réponse avec citation
Vieux 05/12/2006, 23h43   #8
Charles A. Landemaine
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to grab version number?


* Tong * wrote:
> Stephane's answer is exactly what you asked for -- the (first) group of
> numbers separated by dots.
>
> IMHO, when talking about version numbers, what you asked for might not be
> (totally) correct. There might be underlines, or even characters. E.g.,
> here is a (very limited) list of all forms a Debian version number may
> looks like:
>
> foomatic-filters_3.0.2-20061031-1
> gimp_2.2.13-1
> gimp-data_2.2.13-1
> gs-esp_8.15.3.dfsg.1-1
> libcupsys2_1.2.5-1
> libgimp2.0_2.2.13-1


Thanks Tong, good point...

So, we could define the version number by the first string that begins
with and ends with a number, and that has at most a character between
each of its numbers. What do you think?

  Réponse avec citation
Vieux 06/12/2006, 02h03   #9
* Tong *
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to grab version number?

On Tue, 05 Dec 2006 15:43:53 -0800, Charles A. Landemaine wrote:

>> IMHO, when talking about version numbers, what you asked for might not be
>> (totally) correct. There might be underlines, or even characters. E.g.,
>> here is a (very limited) list of all forms a Debian version number may
>> looks like:
>>
>> foomatic-filters_3.0.2-20061031-1
>> gimp_2.2.13-1
>> gimp-data_2.2.13-1
>> gs-esp_8.15.3.dfsg.1-1
>> libcupsys2_1.2.5-1
>> libgimp2.0_2.2.13-1

>
> Thanks Tong, good point...
>
> So, we could define the version number by the first string that begins
> with and ends with a number, and that has at most a character between
> each of its numbers. What do you think?


well, I think it really depends on your input actually.

For example, Debian has s good system, in which version numbers are just
after underscore. In contract, the way RedHat names its packages make it
almost impossible to derive version numbers from package names -- IIRC,
gimp-data_2.2.13-1 would be named as gimp-data-2.2.13...

--
Tong (remove underscore(s) to reply)
http://xpt.sourceforge.net/


--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 06/12/2006, 03h06   #10
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to grab version number?

On 2006-12-05, Charles A. Landemaine wrote:
> I have a string that reports the whole name, version and description of
> an application:
>
> lighttpd-1.4.10 (ssl) - a light and fast webserver
>
> How could I grab only the version, which is only the (first) group of
> numbers separated by dots?


There is no need to use an external command unless you are reading
from a file containing many such strings. A POSIX shell has string
manipulation in parameter expansion:

str='lighttpd-1.4.10 (ssl) - a light and fast webserver'

strl=${str%%[0-9]*} ## str up to first digit
str2=${str#"$strl"} ## str from first digit to end
version=${str2%%[^0-9.]*} ## everything before the first non-digit or period


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 06/12/2006, 08h56   #11
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to grab version number?

2006-12-5, 22:06(-05), Chris F.A. Johnson:
> On 2006-12-05, Charles A. Landemaine wrote:
>> I have a string that reports the whole name, version and description of
>> an application:
>>
>> lighttpd-1.4.10 (ssl) - a light and fast webserver
>>
>> How could I grab only the version, which is only the (first) group of
>> numbers separated by dots?

>
> There is no need to use an external command unless you are reading
> from a file containing many such strings. A POSIX shell has string
> manipulation in parameter expansion:
>
> str='lighttpd-1.4.10 (ssl) - a light and fast webserver'
>
> strl=${str%%[0-9]*} ## str up to first digit
> str2=${str#"$strl"} ## str from first digit to end
> version=${str2%%[^0-9.]*} ## everything before the first non-digit or period

[...]

Wouldn't work for

a2ps-1.2.3

Should be [!0-9.] instead of [^0-9.]

--
Stéphane
  Réponse avec citation
Vieux 06/12/2006, 18h53   #12
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to grab version number?

On 2006-12-06, Stephane CHAZELAS wrote:
> 2006-12-5, 22:06(-05), Chris F.A. Johnson:
>> On 2006-12-05, Charles A. Landemaine wrote:
>>> I have a string that reports the whole name, version and description of
>>> an application:
>>>
>>> lighttpd-1.4.10 (ssl) - a light and fast webserver
>>>
>>> How could I grab only the version, which is only the (first) group of
>>> numbers separated by dots?

>>
>> There is no need to use an external command unless you are reading
>> from a file containing many such strings. A POSIX shell has string
>> manipulation in parameter expansion:
>>
>> str='lighttpd-1.4.10 (ssl) - a light and fast webserver'
>>
>> strl=${str%%[0-9]*} ## str up to first digit
>> str2=${str#"$strl"} ## str from first digit to end
>> version=${str2%%[^0-9.]*} ## everything before the first non-digit or period

> [...]
>
> Wouldn't work for
>
> a2ps-1.2.3
>
> Should be [!0-9.] instead of [^0-9.]


strl=${str%%[0-9][0-9.]*} ## str up to first digit followed by digit or dot
str2=${str#"$strl"} ## str from first digit to end
version=${str2%%[!0-9.]*} ## everything before the first non-digit or period

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 06/12/2006, 19h06   #13
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to grab version number?

2006-12-6, 13:53(-05), Chris F.A. Johnson:
[...]
>> Wouldn't work for
>>
>> a2ps-1.2.3
>>
>> Should be [!0-9.] instead of [^0-9.]

>
> strl=${str%%[0-9][0-9.]*} ## str up to first digit followed by digit or dot
> str2=${str#"$strl"} ## str from first digit to end
> version=${str2%%[!0-9.]*} ## everything before the first non-digit or period


Wouldn't work for mpg123-0.61

I'm afraid it'll have to be even more cumbersome and illegible

Something like:

str='mpg123-0.61 blah'
str1=${str%%[0-9].*}
str2=${str1##*[!0-9]}
str1=${str1%"$str2"}
str2=${str#"$str1"}
version=${str2%%[!0-9.]*}

--
Stéphane
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 16h49.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20019 seconds with 21 queries