-2

I have a word like:

fath

output all combinations of letters in a word like:

f
fa
fat
fath
ft
fth
fh
John Chen
  • 239

2 Answers2

1

EDIT:

Here is a one line awk solution:

echo f a t h | awk '{for(i=0;i<2^NF;i++) { for(j=0;j<NF;j++) {if(and(i,(2^j))) printf "%s",$(j+1)} print ""}}'

Output


f
a
fa
t
ft
at
fat
h
fh
ah
fah
th
fth
ath
fath

(modified version of this power set implementation https://stackoverflow.com/questions/40966428/awk-power-set-implementation)

z3rone
  • 62
1

bash:

combos () {
    local word=$1
    local len=${#word}
    local first=${word:0:1}
    echo "$first"
    for ((i=1; i < len; i++ )); do
        for ((j=1; i+j <= len; j++ )); do
            echo "$first${word:i:j}"
        done
    done
}

then

$ combos fath
f
fa
fat
fath
ft
fth
fh
glenn jackman
  • 85,964