한 줄 쉘은 단어의 모든 문자 조합을 출력합니다.

한 줄 쉘은 단어의 모든 문자 조합을 출력합니다.

제게는 이런 말이 있습니다.

fath

단어의 모든 문자 조합을 출력합니다. 예:

f
fa
fat
fath
ft
fth
fh

답변1

편집하다:

한 줄 awk해결책은 다음과 같습니다.

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 ""}}'

산출


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

(이 전원 세트 구현의 수정된 버전https://stackoverflow.com/questions/40966428/awk-power-set-implementation)

답변2

큰 타격:

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
}

그 다음에

$ combos fath
f
fa
fat
fath
ft
fth
fh

관련 정보