|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Bill Marcum wrote:
> sed 's/[^-]*-\([^ ]*\).*/\1/ Incredible! It works! Thanks, Bill ![]() |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Thank you guys, you're geniuses!
|
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
* 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? |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|