相关疑难解决方法(0)

在UNIX中查找包含字符的所有单词

给定一个单词W,我想从/ usr/dict/words中找到包含W中字母的所有单词.例如,"bat"应该返回"bat"和"tab"(但不是"table").

这是一个涉及对输入词进行排序和匹配的解决方案:

word=$1
sortedWord=`echo $word | grep -o . | sort | tr -d '\n'`

while read line
do
    sortedLine=`echo $line | grep -o . | sort | tr -d '\n'`
    if [ "$sortedWord" == "$sortedLine" ]
    then
        echo $line
    fi
done < /usr/dict/words
Run Code Online (Sandbox Code Playgroud)

有没有更好的办法?我更喜欢使用基本命令(而不是perl/awk等),但欢迎所有解决方案!

为了澄清,我想找到原始单词的所有排列.不允许添加或删除字符.

unix shell

10
推荐指数
1
解决办法
4333
查看次数

标签 统计

shell ×1

unix ×1