Parsing comma-separated digits in ksh

The name of the pictureThe name of the pictureThe name of the pictureClash 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] and n=a[2]






share|improve this question





















  • 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














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] and n=a[2]






share|improve this question





















  • 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












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] and n=a[2]






share|improve this question













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] and n=a[2]








share|improve this question












share|improve this question




share|improve this question








edited May 9 at 18:00









Gilles

503k1189951522




503k1189951522









asked May 9 at 16:16









Jay

12




12











  • 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
















  • 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















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










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.






share|improve this answer























    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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.






    share|improve this answer



























      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.






      share|improve this answer

























        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.






        share|improve this answer















        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.







        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited May 20 at 8:33


























        answered May 9 at 16:27









        Kusalananda

        102k13199315




        102k13199315






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)