[ale] If there a neat way of peeling apart bash variable by delimters?

Ed Cashin ecashin at noserose.net
Mon Sep 15 08:18:21 EDT 2014


Yes, Mike's right that you asked for a bash way and he gave you one.

If, though, you decide you would rather maintain compatibility with
minimal POSIX shells that don't have full bash support, you can use
the old-school Bourne shell features, $IFS and "set".  I think IFS
stands for "internal field separator."

Just set the field separator to your field separator.  After calling
"set" with the string to peel apart, the numbered variables will
correspond to the peeled-apart fields.

In the transcript below I'm checking that it works in the Korn shell
after verifying it in bash beforehand.

  bash$ PS1='ksh$ ' ksh
  ksh$ name=First.Last
  ksh$ IFS=.
  ksh$ set $name
  ksh$ echo $1
  First
  ksh$ echo $2
  Last
  ksh$ ^D
  bash$




On Sun, Sep 14, 2014 at 9:38 PM, Michael H. Warfield <mhw at wittsend.com> wrote:
> On Sun, 2014-09-14 at 19:41 -0400, Neal Rhodes wrote:
>> If in a shell script, you have a variable loaded with a character value,
>> and there is a delimiter, and you want to peel it apart by that
>> delimiter, is there a built-in expression to do that?
>
>> Here's what I'm doing, and I'm thinking there should be a better way.
>
>>         FULLNAME=$1                                  #eg:
>> CustomerEntry.CreditLimit
>>         DOT=`expr index $FULLNAME .`
>>         DOTM1=`expr $DOT - 1`
>>         TABLE=${FULLNAME:0:$DOTM1}
>>         FIELD=${FULLNAME:$DOT:40}
>
> Yes there is.  You specifically said "bash" in the subject and in the
> talks we've had a ALE and AUUG over "belts and suspenders bash" the
> speaker mentioned using regex's in bash and accessing the underlying
> substring results.  Look for BASH_REMATCH variables and the =~ operator
> in the bash documentation.
>
> --
> FOO=CustomerEntry.CreditLimit
>
> if [[ "${FOO}" =~ (.*)\.(.*) ]] ; then
>         echo "${BASH_REMATCH[1]} : ${BASH_REMATCH[2]}"
> else
>         echo nope
> fi
>
> CustomerEntry : CreditLimit
> --
>
> Is that what you're after?
>
>> Neal Rhodes
>> President, MNOP Ltd
>> Lilburn, GA
>> 770-972-5430
>> -------------- next part --------------
>> An HTML attachment was scrubbed...
>> URL: <http://mail.ale.org/pipermail/ale/attachments/20140914/3c89902c/attachment.html>
>
> Regards,
> Mike
> --
> Michael H. Warfield (AI4NB) | (770) 978-7061 |  mhw at WittsEnd.com
>    /\/\|=mhw=|\/\/          | (678) 463-0932 |  http://www.wittsend.com/mhw/
>    NIC whois: MHW9          | An optimist believes we live in the best of all
>  PGP Key: 0x674627FF        | possible worlds.  A pessimist is sure of it!
>
> -------------- next part --------------
> A non-text attachment was scrubbed...
> Name: signature.asc
> Type: application/pgp-signature
> Size: 465 bytes
> Desc: This is a digitally signed message part
> URL: <http://mail.ale.org/pipermail/ale/attachments/20140914/e2365bf0/attachment.sig>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo



-- 
  Ed Cashin <ecashin at noserose.net>


More information about the Ale mailing list