|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
"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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|