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

Jim Kinney jim.kinney at gmail.com
Mon Sep 15 22:40:22 EDT 2014


A bash equivalent of perl @array=split (delim, $string);  would be ideal.


string='my,long,string,of,crap'

OFS=$IFS

IFS=',' read -a array <<< "$string"

now extract bits as needed:

echo "${array[0]}"

my

echo "${array[1]}"

long

etc.




On Sep 15, 2014 9:28 PM, "Neal Rhodes" <neal at mnopltd.com> wrote:

Thanks.  That is a smidge more compact.  Although it doesn't quite jump
> off the page w.r.t. readability.
>
> I was hoping for something along the lines of Progress:
>         Table = entry(1, FullName, ".").
>
> But I shall take a look at BASH_REMATCH.
>
> On Sun, 2014-09-14 at 21:38 -0400, Michael H. Warfield 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
>
>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.ale.org/pipermail/ale/attachments/20140915/8848b893/attachment.html
> >
> _______________________________________________
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.ale.org/pipermail/ale/attachments/20140915/a361745b/attachment.html>


More information about the Ale mailing list