Thursday, August 9, 2018

Generate combinations from a item list in BASH + Ignore same pairs

View the contents of the file using cat command

$ cat list
Pair1
Pair2
Pair3
Pair4

A bash "for" loop in another "for" helps + AWK resolves the issue

$ for d in $(cat list); do for i in $(cat list); do echo "$d $i"; done done | awk '$1 != $2'

Pair1 Pair2
Pair1 Pair3
Pair1 Pair4
Pair2 Pair1
Pair2 Pair3
Pair2 Pair4
Pair3 Pair1
Pair3 Pair2
Pair3 Pair4
Pair4 Pair1
Pair4 Pair2
Pair4 Pair3

No comments:

Post a Comment