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 > finding position of 1st occurence of a [0-9] char in a string
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

finding position of 1st occurence of a [0-9] char in a string

Réponse
 
LinkBack Outils de la discussion
Vieux 08/11/2006, 11h01   #1
Dundonald
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut finding position of 1st occurence of a [0-9] char in a string

A string [a-zA-Z]*[0-9]* eg "Some_Name_1.85.1.35". The end result I
want is to obtain only the non numeric characters from the start of the
string up until the 1st occurence of a numeric character ("Some_Name_"
using the example). But how best to do this? If I can get the
position of the first numeric character I can do expr substr. Though
there may be a better and more complete solution with the likes of awk,
but I'm still learning awk.

  Réponse avec citation
Vieux 08/11/2006, 11h23   #2
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: finding position of 1st occurence of a [0-9] char in a string

2006-11-8, 03:01(-08), Dundonald:
> A string [a-zA-Z]*[0-9]* eg "Some_Name_1.85.1.35". The end result I
> want is to obtain only the non numeric characters from the start of the
> string up until the 1st occurence of a numeric character ("Some_Name_"
> using the example). But how best to do this? If I can get the
> position of the first numeric character I can do expr substr. Though
> there may be a better and more complete solution with the likes of awk,
> but I'm still learning awk.


Assuming a Unix sh conformant shell:

string="Some_Name_1.85.1.35"
newstring=${string%%[0-9]*}

With any shell:

expr "x$string" : 'x\([^0-9]*\)'

"expr substr" is not standard.

--
Stéphane
  Réponse avec citation
Vieux 08/11/2006, 13h54   #3
Dundonald
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: finding position of 1st occurence of a [0-9] char in a string


Stephane CHAZELAS wrote:
> 2006-11-8, 03:01(-08), Dundonald:
> > A string [a-zA-Z]*[0-9]* eg "Some_Name_1.85.1.35". The end result I
> > want is to obtain only the non numeric characters from the start of the
> > string up until the 1st occurence of a numeric character ("Some_Name_"
> > using the example). But how best to do this? If I can get the
> > position of the first numeric character I can do expr substr. Though
> > there may be a better and more complete solution with the likes of awk,
> > but I'm still learning awk.

>
> Assuming a Unix sh conformant shell:
>
> string="Some_Name_1.85.1.35"
> newstring=${string%%[0-9]*}


Thanks Stephane. Is it possible for you to explain how the above works
or pointing me in the right direction (rather than me blindly using it
and not learning anything)? Ta.

>
> With any shell:
>
> expr "x$string" : 'x\([^0-9]*\)'
>
> "expr substr" is not standard.
>
> --
> Stéphane


  Réponse avec citation
Vieux 08/11/2006, 14h01   #4
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: finding position of 1st occurence of a [0-9] char in a string

2006-11-8, 05:54(-08), Dundonald:
>
> Stephane CHAZELAS wrote:
>> 2006-11-8, 03:01(-08), Dundonald:
>> > A string [a-zA-Z]*[0-9]* eg "Some_Name_1.85.1.35". The end result I
>> > want is to obtain only the non numeric characters from the start of the
>> > string up until the 1st occurence of a numeric character ("Some_Name_"
>> > using the example). But how best to do this? If I can get the
>> > position of the first numeric character I can do expr substr. Though
>> > there may be a better and more complete solution with the likes of awk,
>> > but I'm still learning awk.

>>
>> Assuming a Unix sh conformant shell:
>>
>> string="Some_Name_1.85.1.35"
>> newstring=${string%%[0-9]*}

>
> Thanks Stephane. Is it possible for you to explain how the above works
> or pointing me in the right direction (rather than me blindly using it
> and not learning anything)? Ta.

[...]

See the details about ${var%%word} in your shell manual or at
http://www.opengroup.org/onlinepubs/...l#tag_02_06_02

--
Stéphane
  Réponse avec citation
Vieux 08/11/2006, 17h17   #5
Dundonald
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: finding position of 1st occurence of a [0-9] char in a string


Stephane CHAZELAS wrote:
> 2006-11-8, 05:54(-08), Dundonald:
> >
> > Stephane CHAZELAS wrote:
> >> 2006-11-8, 03:01(-08), Dundonald:
> >> > A string [a-zA-Z]*[0-9]* eg "Some_Name_1.85.1.35". The end result I
> >> > want is to obtain only the non numeric characters from the start of the
> >> > string up until the 1st occurence of a numeric character ("Some_Name_"
> >> > using the example). But how best to do this? If I can get the
> >> > position of the first numeric character I can do expr substr. Though
> >> > there may be a better and more complete solution with the likes of awk,
> >> > but I'm still learning awk.
> >>
> >> Assuming a Unix sh conformant shell:
> >>
> >> string="Some_Name_1.85.1.35"
> >> newstring=${string%%[0-9]*}

> >
> > Thanks Stephane. Is it possible for you to explain how the above works
> > or pointing me in the right direction (rather than me blindly using it
> > and not learning anything)? Ta.

> [...]
>
> See the details about ${var%%word} in your shell manual or at
> http://www.opengroup.org/onlinepubs/...l#tag_02_06_02


Excellent resource, thanks.

  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 19h58.


É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,12914 seconds with 13 queries