help-bash
[Top][All Lists]
Advanced

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

Re: Splitting variable into two numbers


From: Leonid Isaev (ifax)
Subject: Re: Splitting variable into two numbers
Date: Tue, 20 Jul 2021 17:01:54 +0000

On Tue, Jul 20, 2021 at 06:15:32PM +0200, lisa-asket@perso.be wrote:
> I would like to split a variable containing two numbers delimited by a comma
> and transfer the two numbers into two variables p and q.
> Input:
> var="{5,8}"

This problem is now well defined because you don't discuss oughter delimiters
("{" and "}" in the above example). Also, one needs to specify if white-space
characters (space/TAB) are allowed arround ",".

So, assuming that the curly braces are indeed used to enclose the payload, one
way is to do a regex match:
-----8<-----
#-- init our variables
p=""
q=""

#-- extract numbers
if [[ "${var//[ $'\t']/}" =~ \{([0-9]*),([0-9]*)\} ]]; then
        p="${BASH_REMATCH[1]}"
        q="${BASH_REMATCH[2]}"
fi

#-- check
if [[ (-n "$p") && (-n "$q") ]]; then
        : do something
fi
----->8-----

HTH,

-- 
Leonid Isaev



reply via email to

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