How to save keys and values from a text file into two separate arrays?
Clash 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.
bash shell-script text-processing awk
add a comment |Â
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.
bash shell-script text-processing awk
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... Withbash
4 usemapfile -t names < <( cut -d: -f1 scores.txt) )
andmapfile -t scores < <(cut -d: -f2 scores.txt | tr -d ' ')
.
â don_crissti
Oct 27 '17 at 22:16
add a comment |Â
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.
bash shell-script text-processing awk
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.
bash shell-script text-processing awk
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... Withbash
4 usemapfile -t names < <( cut -d: -f1 scores.txt) )
andmapfile -t scores < <(cut -d: -f2 scores.txt | tr -d ' ')
.
â don_crissti
Oct 27 '17 at 22:16
add a comment |Â
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... Withbash
4 usemapfile -t names < <( cut -d: -f1 scores.txt) )
andmapfile -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
add a comment |Â
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"
add a comment |Â
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"
add a comment |Â
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"
add a comment |Â
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"
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"
answered Oct 27 '17 at 21:52
Jesse_b
10.5k22659
10.5k22659
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%2f400970%2fhow-to-save-keys-and-values-from-a-text-file-into-two-separate-arrays%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
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 usemapfile -t names < <( cut -d: -f1 scores.txt) )
andmapfile -t scores < <(cut -d: -f2 scores.txt | tr -d ' ')
.â don_crissti
Oct 27 '17 at 22:16