Reputation: 69
I'm trying to write a shell script that takes three lists of names and comes up with every combination from the lists and each list might have a different number of names on it.
List 1
Mike
Tom
Harry
Steve
List 2
Deborah
Sarah
Jennifer
List 3
Alex
Joe
Kelly
Amanda
Will
Phillip
David
It would take Mike from list 1 and then Deborah from list 2 then list all the names from list 3. Then Mike from list 1 again, Sarah from list 2, then all the names from list 3 etc until it's come up with every combination possible.
Having some difficulty thinking of how I would accomplish this and any help would be appreciated.
Upvotes: 0
Views: 89
Reputation: 8995
You can use the for loop. Let f1, f2 and f3 be the files containing the three lists. Then:
for a in `cat f1`;do
for b in `cat f2`;do
for c in `cat f3`;do
echo $a $b $c;
done;
done;
done
For example:
$ cat f1
red
green
cat
$ cat f2
rice
bread
cat f
$ cat f3
tomato
onion
$ for a in `cat f1`;do for b in `cat f2`;do for c in `cat f3`;do echo $a $b $c; done; done; done
red rice tomato
red rice onion
red bread tomato
red bread onion
green rice tomato
green rice onion
green bread tomato
Upvotes: 2
Reputation: 84559
Depending on how you have list x
stored, you can simply use brace expansion to permute all three lists together, e.g.:
printf "%s\n" {Mike,Tom,Harry,Steve}\
{Deborah,Sarah,Jennifer}\
{Alex,Joe,Kelly,Amanda,Will,Phillip,David}
Example Use/Output
$ bash brexpperm.sh
MikeDeborahAlex
MikeDeborahJoe
MikeDeborahKelly
MikeDeborahAmanda
MikeDeborahWill
MikeDeborahPhillip
MikeDeborahDavid
MikeSarahAlex
MikeSarahJoe
MikeSarahKelly
MikeSarahAmanda
MikeSarahWill
MikeSarahPhillip
MikeSarahDavid
MikeJenniferAlex
MikeJenniferJoe
MikeJenniferKelly
MikeJenniferAmanda
MikeJenniferWill
MikeJenniferPhillip
MikeJenniferDavid
TomDeborahAlex
TomDeborahJoe
TomDeborahKelly
TomDeborahAmanda
TomDeborahWill
TomDeborahPhillip
TomDeborahDavid
TomSarahAlex
TomSarahJoe
TomSarahKelly
TomSarahAmanda
TomSarahWill
TomSarahPhillip
TomSarahDavid
TomJenniferAlex
TomJenniferJoe
TomJenniferKelly
TomJenniferAmanda
TomJenniferWill
TomJenniferPhillip
TomJenniferDavid
HarryDeborahAlex
HarryDeborahJoe
HarryDeborahKelly
HarryDeborahAmanda
HarryDeborahWill
HarryDeborahPhillip
HarryDeborahDavid
HarrySarahAlex
HarrySarahJoe
HarrySarahKelly
HarrySarahAmanda
HarrySarahWill
HarrySarahPhillip
HarrySarahDavid
HarryJenniferAlex
HarryJenniferJoe
HarryJenniferKelly
HarryJenniferAmanda
HarryJenniferWill
HarryJenniferPhillip
HarryJenniferDavid
SteveDeborahAlex
SteveDeborahJoe
SteveDeborahKelly
SteveDeborahAmanda
SteveDeborahWill
SteveDeborahPhillip
SteveDeborahDavid
SteveSarahAlex
SteveSarahJoe
SteveSarahKelly
SteveSarahAmanda
SteveSarahWill
SteveSarahPhillip
SteveSarahDavid
SteveJenniferAlex
SteveJenniferJoe
SteveJenniferKelly
SteveJenniferAmanda
SteveJenniferWill
SteveJenniferPhillip
SteveJenniferDavid
Or, if you need a space, you can simply add one to the expansion:
printf "%s\n" {'Mike ','Tom ','Harry ','Steve '}\
{'Deborah ','Sarah ','Jennifer '}\
{Alex,Joe,Kelly,Amanda,Will,Phillip,David}
Example Use/Output
$ bash brexpperm.sh
Mike Deborah Alex
Mike Deborah Joe
Mike Deborah Kelly
Mike Deborah Amanda
...
Steve Jennifer Amanda
Steve Jennifer Will
Steve Jennifer Phillip
Steve Jennifer David
If you do not have control over the lists within your script itself, then a loop solution works fine.
Upvotes: 1