I have a list and I want to randomly select one text string for every unique prefix. For example, here's my list:
apples_1
apples_2
apples_3
banana_1
banana_2
pears_3
For each unique prefix (apples, banana, pears) I want to randomly select one. The desired output would then be:
apples_3
banana_1
pears_3
I've seen similar posts here and here on SO using arrays but it's unclear to me how to apply those answers here. I'm completely lost on how to go about doing this. Any suggestions to get me started would be greatly appreciated.
EDIT: per the user comment to show what I've tried:
- Attempting to apply the SO arrays links above:
ARRAY=(filename.txt)
N1=$((RANDOM % 5))
SDFFILE=${ARRAY[$N1]}
echo $SDFFILE_MIN
Per the posts, I assumed the above would return 5 random lines of text and I would attempt to build out from there. Nothing happened and I couldn't follow those answers to troubleshoot.
- Then I starting thinking I would split my text strings and select one of the unique prefix lines.
cut -d'_' -f 1
Above returns all the prefixes (duplicates now) but obviously if I subsample from that list it won't be subsampling as it would only return:
apples
banana
pears
I'm having a hard time thinking through how to implement this. Thanks.
Aucun commentaire:
Enregistrer un commentaire