在bash中迭代用户字符串以将元音添加到字符串

时间:2016-12-08 18:49:24

标签: bash loops unix word

所以我有一个包含超过30,000个单词的单词列表。我的目标是创建一个脚本,其中包含一个没有常量的单词(例如:mbnt),并以某种方式添加常量并与单词列表进行比较以找到至少单词“ambient”,尽管它也会找到其他单词如果要取出所有的元音,请将其读作“mbnt”。 到目前为止,这是我的bash脚本

f=/wordList
anyVowel=[aAeEiIoOuU]
nonVowel=[^aAeEiIoOuU]
input=$1
for (( i=0; i<${#input}; i++ ));
do
grep "${input:$i:1}$nonVowel" $f | head -10
done

然而,这只会返回一个正常的单词列表,其中包含用户输入的一些字符。关于我可能做错什么的任何想法?

2 个答案:

答案 0 :(得分:2)

awk救援!

$ awk -v w=whr '{a=tolower($0); 
                 gsub(/[^a-z]/,"",a); 
                 gsub(/[aeiou]/,"",a)} 
                a==w' words

where

寻找掉落的元音&#34; whr&#34;用(组成一个自定义词典)。转换为小写,过滤掉非alpha并删除元音,最后查找与给定单词的匹配。

请注意,如果您要查找多个单词,这效率非常低,但也许可以作为解决方案的模板。

答案 1 :(得分:0)

尝试

wordsfile=wordList
consonants=$1

# Create a regular expression that matches the input consonants with
# any number of vowels before, after, or between them
regex='^[[:space:]]*[aeiou]*'
for (( i=0; i<${#consonants}; i++ )) ; do
    regex+="${consonants:i:1}[aeiou]*"
done
regex+='[[:space:]]*$'

grep -i -- "$regex" "$wordsfile"