I have a word like:
fath
output all combinations of letters in a word like:
f
fa
fat
fath
ft
fth
fh
I have a word like:
fath
output all combinations of letters in a word like:
f
fa
fat
fath
ft
fth
fh
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)
ath
is present and the OP asked for combinations, not permutations.
– Ed Morton
Jun 15 '20 at 23:29
sed -e 's/\(.\)/\1 /g'
– z3rone
Jun 17 '20 at 11:41
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