I have a list of groups, in which I have people's names:
group1="Kyliane Justine Noemie Marion" group2="Julie Lilou" group3="Lena Celeste"
and so on...
I want to check how many times the name of a person of a group appears in a file that is named according to the group number (ex: group1.txt).
So I have:
for person in $group1 do number=$(grep -oi "$person" /path/group1.txt | wc -l) echo $person "wrote" $number "times." done
that works fine.
BUT, what I would like, is to check each group members in each group file, not only $group1 in /path/group1.txt, but also $group2 in /path/group2.txt, and so on... I know I can copy-paste my code and just change group1 by group2, group3, and so on. But I'm sure there is a quicker way especially as I have 12 groups! I'm a beginner in bash and I don't know how to do that.
Thanks a lot for your help.
Use an array instead of separate variables.
groups=("Kyliane Justine Noemie Marion" "Julie Lilou" "Lena Celeste") for group in "$groups[@]" do for person in $group do number=$(grep -oi "$person" /path/group1.txt | wc -l) echo $person "wrote" $number "times." done done
As a general rule, whenever you find yourself creating variables with sequential names, you probably should be using an array or similar data structure. This applies across most programming languages.