在UNIX中查找包含字符的所有单词
发布时间:2020-12-16 01:30:52 所属栏目:安全 来源:网络整理
导读:给出一个单词W,我想从/usr/dict / words找到包含W中所有字母的所有单词. 例如,“bat”应该返回“bat”和“tab”(而不是“table”). 这是一个解决方案,其中包括排序输入字和匹配: word=$1sortedWord=`echo $word | grep -o . | sort | tr -d 'n'`while read
给出一个单词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 有没有更好的办法?我更喜欢使用基本命令(而不是perl / awk等),但所有的解决方案都是欢迎的! 为了澄清,我想找到原来的单词的所有排列.不允许添加或删除字符.
这是一个awk实现.它在“W”中找到这些字母的单词.
dict="/usr/share/dict/words" word=$1 awk -vw="$word" 'BEGIN{ m=split(w,c,"") for(p=1;p<=m;p++){ chars[c[p]]++ } } length($0)==length(w){ f=0;g=0 n=split($0,t,"") for(o=1;o<=n;o++){ if (!( t[o] in chars) ){ f=1; break }else{ st[t[o]]++ } } if (!f || $0==w){ for(z in st){ if ( st[z] != chars[z] ) { g=1 ;break} } if(!g){ print "found: "$0 } } delete st }' $dict 产量 $wc -l < /usr/share/dict/words 479829 $time ./shell.sh look found: kolo found: look real 0m1.361s user 0m1.074s sys 0m0.015s 更新:更改算法,使用排序 dict="/usr/share/dict/words" awk 'BEGIN{ w="table" m=split(w,"") b=asort(c,chars) } length($0)==length(w){ f=0 n=split($0,"") e=asort(t,d) for(i=1;i<=e;i++) { if(d[i]!=chars[i]){ f=1;break } } if(!f) print $0 }' $dict 产量 $time ./shell.sh #looking for table ablet batel belat blate bleat tabel table real 0m1.416s user 0m1.343s sys 0m0.014s $time ./shell.sh #looking for chairs chairs ischar rachis real 0m1.697s user 0m1.660s sys 0m0.014s $time perl perl.pl #using beamrider's Perl script table tabel ablet batel blate bleat belat real 0m2.680s user 0m1.633s sys 0m0.881s $time perl perl.pl # looking for chairs chairs ischar rachis real 0m14.044s user 0m8.328s sys 0m5.236s (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |