Parsing comma-separated digits in ksh

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a variable in ksh that can contain no more then 2 comma-separated digits (white spaces allowed). Something like:
a="1,2"
a="1 ,2"
a="1,"
a="0,0"
I want a loop to
- read through
$a - exit if there is more than one
, - assign
m=a[1]andn=a[2]
shell ksh string
add a comment |Â
up vote
0
down vote
favorite
I have a variable in ksh that can contain no more then 2 comma-separated digits (white spaces allowed). Something like:
a="1,2"
a="1 ,2"
a="1,"
a="0,0"
I want a loop to
- read through
$a - exit if there is more than one
, - assign
m=a[1]andn=a[2]
shell ksh string
What doesmore then onemean?
â cuonglm
May 9 at 16:20
if a="1,2,3" or "1,2, " then exit. no more than one comma
â Jay
May 9 at 16:27
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a variable in ksh that can contain no more then 2 comma-separated digits (white spaces allowed). Something like:
a="1,2"
a="1 ,2"
a="1,"
a="0,0"
I want a loop to
- read through
$a - exit if there is more than one
, - assign
m=a[1]andn=a[2]
shell ksh string
I have a variable in ksh that can contain no more then 2 comma-separated digits (white spaces allowed). Something like:
a="1,2"
a="1 ,2"
a="1,"
a="0,0"
I want a loop to
- read through
$a - exit if there is more than one
, - assign
m=a[1]andn=a[2]
shell ksh string
edited May 9 at 18:00
Gilles
503k1189951522
503k1189951522
asked May 9 at 16:16
Jay
12
12
What doesmore then onemean?
â cuonglm
May 9 at 16:20
if a="1,2,3" or "1,2, " then exit. no more than one comma
â Jay
May 9 at 16:27
add a comment |Â
What doesmore then onemean?
â cuonglm
May 9 at 16:20
if a="1,2,3" or "1,2, " then exit. no more than one comma
â Jay
May 9 at 16:27
What does
more then one mean?â cuonglm
May 9 at 16:20
What does
more then one mean?â cuonglm
May 9 at 16:20
if a="1,2,3" or "1,2, " then exit. no more than one comma
â Jay
May 9 at 16:27
if a="1,2,3" or "1,2, " then exit. no more than one comma
â Jay
May 9 at 16:27
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
for a in "1,2" "1 ,2" "1," "0,0" "0,0,0" "2,3"; do
case "$a" in
*,*,*) printf 'Too many commas: "%s"n' "$a" >&2
exit 1
esac
IFS=', ' read m n <<<"$a"
printf 'm = %d, n = %dn' "$m" "$n"
done
This produces
m = 1, n = 2
m = 1, n = 2
m = 1, n = 0
m = 0, n = 0
Too many commas: "0,0,0"
when running under ksh93 (or bash or zsh).
Setting IFS to a space and a comma before read makes read split the contents of $a on these two characters (or multiples of them). The remaining data is assigned to m and n.
If a string is something like "2,3 4", then n would be assigned the value 3 4.
Note also that on the third line of output (parsing "1,"), m is shown as zero only because we're using the %d format specifier with printf. If interpreted as a string, the variable m is empty.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
for a in "1,2" "1 ,2" "1," "0,0" "0,0,0" "2,3"; do
case "$a" in
*,*,*) printf 'Too many commas: "%s"n' "$a" >&2
exit 1
esac
IFS=', ' read m n <<<"$a"
printf 'm = %d, n = %dn' "$m" "$n"
done
This produces
m = 1, n = 2
m = 1, n = 2
m = 1, n = 0
m = 0, n = 0
Too many commas: "0,0,0"
when running under ksh93 (or bash or zsh).
Setting IFS to a space and a comma before read makes read split the contents of $a on these two characters (or multiples of them). The remaining data is assigned to m and n.
If a string is something like "2,3 4", then n would be assigned the value 3 4.
Note also that on the third line of output (parsing "1,"), m is shown as zero only because we're using the %d format specifier with printf. If interpreted as a string, the variable m is empty.
add a comment |Â
up vote
4
down vote
for a in "1,2" "1 ,2" "1," "0,0" "0,0,0" "2,3"; do
case "$a" in
*,*,*) printf 'Too many commas: "%s"n' "$a" >&2
exit 1
esac
IFS=', ' read m n <<<"$a"
printf 'm = %d, n = %dn' "$m" "$n"
done
This produces
m = 1, n = 2
m = 1, n = 2
m = 1, n = 0
m = 0, n = 0
Too many commas: "0,0,0"
when running under ksh93 (or bash or zsh).
Setting IFS to a space and a comma before read makes read split the contents of $a on these two characters (or multiples of them). The remaining data is assigned to m and n.
If a string is something like "2,3 4", then n would be assigned the value 3 4.
Note also that on the third line of output (parsing "1,"), m is shown as zero only because we're using the %d format specifier with printf. If interpreted as a string, the variable m is empty.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
for a in "1,2" "1 ,2" "1," "0,0" "0,0,0" "2,3"; do
case "$a" in
*,*,*) printf 'Too many commas: "%s"n' "$a" >&2
exit 1
esac
IFS=', ' read m n <<<"$a"
printf 'm = %d, n = %dn' "$m" "$n"
done
This produces
m = 1, n = 2
m = 1, n = 2
m = 1, n = 0
m = 0, n = 0
Too many commas: "0,0,0"
when running under ksh93 (or bash or zsh).
Setting IFS to a space and a comma before read makes read split the contents of $a on these two characters (or multiples of them). The remaining data is assigned to m and n.
If a string is something like "2,3 4", then n would be assigned the value 3 4.
Note also that on the third line of output (parsing "1,"), m is shown as zero only because we're using the %d format specifier with printf. If interpreted as a string, the variable m is empty.
for a in "1,2" "1 ,2" "1," "0,0" "0,0,0" "2,3"; do
case "$a" in
*,*,*) printf 'Too many commas: "%s"n' "$a" >&2
exit 1
esac
IFS=', ' read m n <<<"$a"
printf 'm = %d, n = %dn' "$m" "$n"
done
This produces
m = 1, n = 2
m = 1, n = 2
m = 1, n = 0
m = 0, n = 0
Too many commas: "0,0,0"
when running under ksh93 (or bash or zsh).
Setting IFS to a space and a comma before read makes read split the contents of $a on these two characters (or multiples of them). The remaining data is assigned to m and n.
If a string is something like "2,3 4", then n would be assigned the value 3 4.
Note also that on the third line of output (parsing "1,"), m is shown as zero only because we're using the %d format specifier with printf. If interpreted as a string, the variable m is empty.
edited May 20 at 8:33
answered May 9 at 16:27
Kusalananda
102k13199315
102k13199315
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442820%2fparsing-comma-separated-digits-in-ksh%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
What does
more then onemean?â cuonglm
May 9 at 16:20
if a="1,2,3" or "1,2, " then exit. no more than one comma
â Jay
May 9 at 16:27