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 > extract array name from string
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

extract array name from string

Réponse
 
LinkBack Outils de la discussion
Vieux 30/04/2008, 03h40   #1
Eric
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut extract array name from string

I've got a text file with names like this:
Variable1[30]
extraVar22[22]
Test42Exists[104]

how do i get just the array name in each case
e.g.:
N="Variable1[30]"
echo "$N"|sed <what goes here>"
or
echo "$N"|eval match "$N" \(<what goes here>\)"

the output would be Variable1 (dropping the [30])

It can be sed or bash, or whatever, I'm using this in a bash script.
I was playing with echo 'eval match "$N" \( stuff here \)'
but i couldnt get it to work right, array names can be a mix of upper and
lower case, numbers (and possibly underscores).
Thanks
Eric
  Réponse avec citation
Vieux 30/04/2008, 04h19   #2
Rajan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract array name from string



"Eric" <Scorpus@gordinator.org> wrote in message
news:7O6dnSa6OeSKQYrVnZ2dnUVZ_jqdnZ2d@comcast.com. ..
> I've got a text file with names like this:
> Variable1[30]
> extraVar22[22]
> Test42Exists[104]
>
> how do i get just the array name in each case
> e.g.:
> N="Variable1[30]"
> echo "$N"|sed <what goes here>"
> or
> echo "$N"|eval match "$N" \(<what goes here>\)"
>
> the output would be Variable1 (dropping the [30])
>
> It can be sed or bash, or whatever, I'm using this in a bash script.
> I was playing with echo 'eval match "$N" \( stuff here \)'
> but i couldnt get it to work right, array names can be a mix of upper and
> lower case, numbers (and possibly underscores).
> Thanks
> Eric


Did you try a cut?
echo "$N" | cut -d"[" -f1
or just cut -d"[" -f1 yourfile

  Réponse avec citation
Vieux 30/04/2008, 08h59   #3
Dave B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract array name from string

On Wednesday 30 April 2008 04:40, Eric wrote:

> I've got a text file with names like this:
> Variable1[30]
> extraVar22[22]
> Test42Exists[104]
>
> how do i get just the array name in each case
> e.g.:
> N="Variable1[30]"
> echo "$N"|sed <what goes here>"
> or
> echo "$N"|eval match "$N" \(<what goes here>\)"
>
> the output would be Variable1 (dropping the [30])
>
> It can be sed or bash, or whatever, I'm using this in a bash script.
> I was playing with echo 'eval match "$N" \( stuff here \)'
> but i couldnt get it to work right, array names can be a mix of upper and
> lower case, numbers (and possibly underscores).


Plenty of ways to do that, in addition to cut, which was already suggested.

$ printf "%s\n" 'Test42Exists[104]' | awk -F'[' '{print $1}'
Test42Exists

$ printf "%s\n" 'Test42Exists[104]' | sed 's/\[[^]]*\]$//'
Test42Exists

$ expr 'Test42Exists[104]' : '\([^[]*\)'
Test42Exists
$ expr match 'Test42Exists[104]' '\([^[]*\)'
Test42Exists

$ var='Test42Exists[104]'
$ printf "%s\n" "${var%\[*\]}"
Test42Exists

And, since you use bash:

$ var='Test42Exists[104]'
$ printf "%s\n" "${var/%\[*\]/}"
Test42Exists

--
D.
  Réponse avec citation
Vieux 30/04/2008, 09h06   #4
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract array name from string

On 30 Apr., 04:40, Eric <Scor...@gordinator.org> wrote:
> I've got a text file with names like this:
> Variable1[30]
> extraVar22[22]
> Test42Exists[104]
>
> how do i get just the array name in each case
> e.g.:
> N="Variable1[30]"
> echo "$N"|sed <what goes here>"
> or
> echo "$N"|eval match "$N" \(<what goes here>\)"
>
> the output would be Variable1 (dropping the [30])
>
> It can be sed or bash, or whatever, I'm using this in a bash script.
> I was playing with echo 'eval match "$N" \( stuff here \)'
> but i couldnt get it to work right, array names can be a mix of upper and
> lower case, numbers (and possibly underscores).
> Thanks
> Eric


N="Variable1[30]"
echo ${N%%[[]*}


Janis
  Réponse avec citation
Vieux 30/04/2008, 09h13   #5
pk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract array name from string

On Wednesday 30 April 2008 10:06, Janis wrote:

> N="Variable1[30]"
> echo ${N%%[[]*}


Why not just

echo ${N%[*}

then?

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
  Réponse avec citation
Vieux 30/04/2008, 09h34   #6
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract array name from string

On 30 Apr., 10:13, pk <p...@pk.invalid> wrote:
> On Wednesday 30 April 2008 10:06, Janis wrote:
>
> > N="Variable1[30]"
> > echo ${N%%[[]*}

>
> Why not just
>
> echo ${N%[*}
>
> then?


$ sh -c 'N="Variable1[30]"; echo ${N%%[*}'
Variable1[30]
$ sh -c 'N="Variable1[30]"; echo ${N%%[[]*}'
Variable1

More reliable (with other shells).

Janis
  Réponse avec citation
Vieux 30/04/2008, 09h36   #7
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract array name from string

2008-04-30, 10:13(+02), pk:
> On Wednesday 30 April 2008 10:06, Janis wrote:
>
>> N="Variable1[30]"
>> echo ${N%%[[]*}

>
> Why not just
>
> echo ${N%[*}

[...]

$ zsh -c 'echo ${N%[*}'
zsh:1: bad pattern: [*
$ ash -c 'echo ${N%[*}'
Variable1[30]
$ ksh93 -c 'echo ${N%[*}'
Variable1[30]
$ pdksh -c 'echo ${N%[*}'
Variable1[30]
$ bash -c 'echo ${N%[*}'
Variable1

From SUSv3:
An expression containing a '[' that is not preceded by a
backslash and is not part of a bracket expression produces
undefined results.

That's in the RE section, but the globbing spec refers to REs
for the bracket expression.

--
Stéphane
  Réponse avec citation
Vieux 30/04/2008, 09h39   #8
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract array name from string

On 30 Apr., 10:34, Janis <janis_papanag...@hotmail.com> wrote:
> On 30 Apr., 10:13, pk <p...@pk.invalid> wrote:
>
> > On Wednesday 30 April 2008 10:06, Janis wrote:

>
> > > N="Variable1[30]"
> > > echo ${N%%[[]*}

>
> > Why not just

>
> > echo ${N%[*}

>
> > then?

>
> $ sh -c 'N="Variable1[30]"; echo ${N%%[*}'
> Variable1[30]
> $ sh -c 'N="Variable1[30]"; echo ${N%%[[]*}'
> Variable1


Ah, you had two questions in your posting.

$ sh -c 'N="Variable1[30][40]"; echo ${N%[[]*}'
Variable1[30]
$ sh -c 'N="Variable1[30][40]"; echo ${N%%[[]*}'
Variable1

I used double %% because (depending on the parsed
language) arrays can possibly have multiple dimensions.

>
> More reliable (with other shells).
>
> Janis


  Réponse avec citation
Vieux 30/04/2008, 10h25   #9
pk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract array name from string

On Wednesday 30 April 2008 10:39, Janis wrote:

> I used double %% because (depending on the parsed
> language) arrays can possibly have multiple dimensions.


I did not think about that, since the OP only posted examples of
single-dimension arrays.

Thanks!

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
  Réponse avec citation
Vieux 30/04/2008, 10h26   #10
pk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract array name from string

On Wednesday 30 April 2008 10:36, Stephane CHAZELAS wrote:

> From SUSv3:
> An expression containing a '[' that is not preceded by a
> backslash and is not part of a bracket expression produces
> undefined results.


Ah ok, I just stumbled upon another bash peculiarity.
Thanks!

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
  Réponse avec citation
Vieux 01/05/2008, 06h36   #11
Eric
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract array name from string

Eric wrote:

> I've got a text file with names like this:
> Variable1[30]
> extraVar22[22]
> Test42Exists[104]
>
> how do i get just the array name in each case
> e.g.:
> N="Variable1[30]"
> echo "$N"|sed <what goes here>"
> or
> echo "$N"|eval match "$N" \(<what goes here>\)"
>
> the output would be Variable1 (dropping the [30])
>
> It can be sed or bash, or whatever, I'm using this in a bash script.
> I was playing with echo 'eval match "$N" \( stuff here \)'
> but i couldnt get it to work right, array names can be a mix of upper and
> lower case, numbers (and possibly underscores).
> Thanks
> Eric

Thanks all, I finally came up with this by experiment:
arrName=$(echo `expr match "$N" '\([A-Za-z0−9]*\)'`)
but I'll probably replace it with something from replies to my post,
your suggestions seem more to the point.
Thanks,
Eric
  Réponse avec citation
Vieux 01/05/2008, 10h13   #12
Dave B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract array name from string

On Thursday 1 May 2008 07:36, Eric wrote:

> Thanks all, I finally came up with this by experiment:
> arrName=$(echo `expr match "$N" '\([A-Za-z0−9]*\)'`)


arrName=$(expr match "$N" '\([A-Za-z0−9]*\)')

Consider using [[:alnum:]] instead of [A-Za-z0−9]. Finally, underscores
("_") and possibly other characters are probably allowed in names.

--
D.
  Réponse avec citation
Vieux 01/05/2008, 19h15   #13
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract array name from string

Eric wrote:
> Eric wrote:
>
>
>>I've got a text file with names like this:
>> Variable1[30]
>> extraVar22[22]
>> Test42Exists[104]
>>
>>how do i get just the array name in each case
>>e.g.:
>> N="Variable1[30]"
>> echo "$N"|sed <what goes here>"
>>or
>> echo "$N"|eval match "$N" \(<what goes here>\)"
>>
>>the output would be Variable1 (dropping the [30])
>>
>>It can be sed or bash, or whatever, I'm using this in a bash script.
>>I was playing with echo 'eval match "$N" \( stuff here \)'
>>but i couldnt get it to work right, array names can be a mix of upper and
>>lower case, numbers (and possibly underscores).
>>Thanks
>>Eric

>
> Thanks all, I finally came up with this by experiment:
> arrName=$(echo `expr match "$N" '\([A-Za-z0−9]*\)'`)


Why this double program substitution $(echo `...`) instead of using
just one?

arrName=$(expr match "$N" '\([A-Za-z0-9]*\)')

Though I'd use something simpler than that, anyway.

> but I'll probably replace it with something from replies to my post,
> your suggestions seem more to the point.
> Thanks,
> Eric

  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 07h00.


É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,16310 seconds with 21 queries