Generating permutations using bash

Viewed 39016

is it possible to write a bash script that can read in each line from a file and generate permutations (without repetition) for each? Using awk / perl is fine.

File
----
ab
abc


Output
------
ab
ba
abc
acb
bac
bca
cab
cba
9 Answers

file named input:

a
b
c
d

If you want the output:

a b
a c
a d
b b
b c
b d
c c
c d
d d

You can try the following bash script:

lines=$(wc -l input | awk '{print $1}')
for ((i=1 ; i<=$lines ; i++)); do
        x=$(sed -n ''$i' p' input)
        sed -n ''$i',$ p' input > tmp
        for j in $(cat tmp) ; do
                echo $x $j
        done
done
Related