How to save keys and values from a text file into two separate arrays?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have a text file for keeping game scores, the format is thus:



Name: score


Using a Bash script, I'm trying to place the names in one array and the scores in another. My first approach used the cut command:



names=(cut -d: -f1 ./scores.txt)
scores=(cut -d: -f2 ./scores.txt)


However, this approach didn't quite work because it would put all the names and scores in the very first entry in the array, which is inconvenient since I want to put the top five values in different variables and do a host of other things with them. I then tried using the following awk command:



names=(awk -F: ' print $0 ' ./scores.txt)
scores=(awk -F: ' print $1 ' ./scores.txt)


This did the exact same thing.



Does anyone have any suggestions on how to put all the parsed values in their own array element, or perhaps a completely different approach to efficiently store these values? Also, this has to be done in Bash for reasons.







share|improve this question






















  • Would it make sense to put them both into one associative array in which the index could be the name and the element could be the score?
    – Jesse_b
    Oct 27 '17 at 21:46










  • That's not how you save the output into an array... With bash 4 use mapfile -t names < <( cut -d: -f1 scores.txt) ) and mapfile -t scores < <(cut -d: -f2 scores.txt | tr -d ' ').
    – don_crissti
    Oct 27 '17 at 22:16














up vote
1
down vote

favorite












I have a text file for keeping game scores, the format is thus:



Name: score


Using a Bash script, I'm trying to place the names in one array and the scores in another. My first approach used the cut command:



names=(cut -d: -f1 ./scores.txt)
scores=(cut -d: -f2 ./scores.txt)


However, this approach didn't quite work because it would put all the names and scores in the very first entry in the array, which is inconvenient since I want to put the top five values in different variables and do a host of other things with them. I then tried using the following awk command:



names=(awk -F: ' print $0 ' ./scores.txt)
scores=(awk -F: ' print $1 ' ./scores.txt)


This did the exact same thing.



Does anyone have any suggestions on how to put all the parsed values in their own array element, or perhaps a completely different approach to efficiently store these values? Also, this has to be done in Bash for reasons.







share|improve this question






















  • Would it make sense to put them both into one associative array in which the index could be the name and the element could be the score?
    – Jesse_b
    Oct 27 '17 at 21:46










  • That's not how you save the output into an array... With bash 4 use mapfile -t names < <( cut -d: -f1 scores.txt) ) and mapfile -t scores < <(cut -d: -f2 scores.txt | tr -d ' ').
    – don_crissti
    Oct 27 '17 at 22:16












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a text file for keeping game scores, the format is thus:



Name: score


Using a Bash script, I'm trying to place the names in one array and the scores in another. My first approach used the cut command:



names=(cut -d: -f1 ./scores.txt)
scores=(cut -d: -f2 ./scores.txt)


However, this approach didn't quite work because it would put all the names and scores in the very first entry in the array, which is inconvenient since I want to put the top five values in different variables and do a host of other things with them. I then tried using the following awk command:



names=(awk -F: ' print $0 ' ./scores.txt)
scores=(awk -F: ' print $1 ' ./scores.txt)


This did the exact same thing.



Does anyone have any suggestions on how to put all the parsed values in their own array element, or perhaps a completely different approach to efficiently store these values? Also, this has to be done in Bash for reasons.







share|improve this question














I have a text file for keeping game scores, the format is thus:



Name: score


Using a Bash script, I'm trying to place the names in one array and the scores in another. My first approach used the cut command:



names=(cut -d: -f1 ./scores.txt)
scores=(cut -d: -f2 ./scores.txt)


However, this approach didn't quite work because it would put all the names and scores in the very first entry in the array, which is inconvenient since I want to put the top five values in different variables and do a host of other things with them. I then tried using the following awk command:



names=(awk -F: ' print $0 ' ./scores.txt)
scores=(awk -F: ' print $1 ' ./scores.txt)


This did the exact same thing.



Does anyone have any suggestions on how to put all the parsed values in their own array element, or perhaps a completely different approach to efficiently store these values? Also, this has to be done in Bash for reasons.









share|improve this question













share|improve this question




share|improve this question








edited Oct 27 '17 at 21:54









don_crissti

46.9k15124154




46.9k15124154










asked Oct 27 '17 at 21:44









LordHoratio

83




83











  • Would it make sense to put them both into one associative array in which the index could be the name and the element could be the score?
    – Jesse_b
    Oct 27 '17 at 21:46










  • That's not how you save the output into an array... With bash 4 use mapfile -t names < <( cut -d: -f1 scores.txt) ) and mapfile -t scores < <(cut -d: -f2 scores.txt | tr -d ' ').
    – don_crissti
    Oct 27 '17 at 22:16
















  • Would it make sense to put them both into one associative array in which the index could be the name and the element could be the score?
    – Jesse_b
    Oct 27 '17 at 21:46










  • That's not how you save the output into an array... With bash 4 use mapfile -t names < <( cut -d: -f1 scores.txt) ) and mapfile -t scores < <(cut -d: -f2 scores.txt | tr -d ' ').
    – don_crissti
    Oct 27 '17 at 22:16















Would it make sense to put them both into one associative array in which the index could be the name and the element could be the score?
– Jesse_b
Oct 27 '17 at 21:46




Would it make sense to put them both into one associative array in which the index could be the name and the element could be the score?
– Jesse_b
Oct 27 '17 at 21:46












That's not how you save the output into an array... With bash 4 use mapfile -t names < <( cut -d: -f1 scores.txt) ) and mapfile -t scores < <(cut -d: -f2 scores.txt | tr -d ' ').
– don_crissti
Oct 27 '17 at 22:16




That's not how you save the output into an array... With bash 4 use mapfile -t names < <( cut -d: -f1 scores.txt) ) and mapfile -t scores < <(cut -d: -f2 scores.txt | tr -d ' ').
– don_crissti
Oct 27 '17 at 22:16










1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Assuming your input file has every name/score combo on a newline this should do what you need:



while read line; do 
names+=($(echo "$line" | awk 'print $1' | tr -d ':'))
scores+=($(echo "$line" | awk 'print $2'))
done < "$INPUT_FILE"





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%2f400970%2fhow-to-save-keys-and-values-from-a-text-file-into-two-separate-arrays%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
    0
    down vote



    accepted










    Assuming your input file has every name/score combo on a newline this should do what you need:



    while read line; do 
    names+=($(echo "$line" | awk 'print $1' | tr -d ':'))
    scores+=($(echo "$line" | awk 'print $2'))
    done < "$INPUT_FILE"





    share|improve this answer
























      up vote
      0
      down vote



      accepted










      Assuming your input file has every name/score combo on a newline this should do what you need:



      while read line; do 
      names+=($(echo "$line" | awk 'print $1' | tr -d ':'))
      scores+=($(echo "$line" | awk 'print $2'))
      done < "$INPUT_FILE"





      share|improve this answer






















        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        Assuming your input file has every name/score combo on a newline this should do what you need:



        while read line; do 
        names+=($(echo "$line" | awk 'print $1' | tr -d ':'))
        scores+=($(echo "$line" | awk 'print $2'))
        done < "$INPUT_FILE"





        share|improve this answer












        Assuming your input file has every name/score combo on a newline this should do what you need:



        while read line; do 
        names+=($(echo "$line" | awk 'print $1' | tr -d ':'))
        scores+=($(echo "$line" | awk 'print $2'))
        done < "$INPUT_FILE"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 27 '17 at 21:52









        Jesse_b

        10.5k22659




        10.5k22659



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f400970%2fhow-to-save-keys-and-values-from-a-text-file-into-two-separate-arrays%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?