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
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