bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Grepping an array in bash, odd/buggy behavior when using shopt and '


From: Greg Wooledge
Subject: Re: Grepping an array in bash, odd/buggy behavior when using shopt and '!'
Date: Wed, 3 Feb 2010 08:45:08 -0500
User-agent: Mutt/1.4.2.3i

On Tue, Feb 02, 2010 at 07:29:25PM -0800, Davey E wrote:
> FOO[3]="ABZ555"

> shopt -s extglob
> echo 'address@hidden/#!(ABZ555)/}' results in:  address@hidden/#!(ABZ555)/}

> When run, I see:
> address@hidden/#!(ABZ555)/} results in: 5

You are trying to use a substitution operator to perform matching, which
is not what it's intended for.

${parameter/pattern/string} means "expand parameter, replacing the first
and longest occurrence of pattern with string".  A leading # forces the
pattern to be anchored at the start of the parameter.  The extended glob
notation !(foo) means "anything other than foo".

ABZ55 is a string which is not ABZ555.  In fact, it's the longest
such match.  And so your ${//} is replacing ABZ55 at the start of
the parameter with an empty string.  This is the documented behavior.

It's pretty unclear to me what your example is supposed to represent.
If you're searching a large associative array's values (as opposed to
keys) for specific patterns, perhaps you need to consider rewriting
this application in a language that has actual database support, and
then using an indexed database to do the searches.  Associative arrays
were never meant to have their values searched, and any such search will
have to be done by brute force.

Another issue with your example is that you're attempting to emit all
the matched/unmatched elements in a single command that gives back
one string.  If your elements contain whitespace, you'll never be able
to tell how many elements are in the resulting string, or where they
are delimited.  Perhaps that's not an issue with your actual data set,
but it's impossible to tell from the obfuscated example.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]