【发布时间】:2017-08-09 16:21:40
【问题描述】:
我想显示正好有 14、15 和 16 个唯一字母的单词的数量。我想使用 for 循环。 (它必须是一个衬里。)
这是我目前所拥有的:for i in {14..16}; do echo "There are $(cat /usr/share/dict/dutch | grep -P '^.{"$i"}$' | grep -vP -c '(.).*\1') words with exactly $i unique letters"; done
结果:
There are 0 words with exactly 14 unique lettersThere are 0 words with exactly 15 unique lettersThere are 0 words with exactly 16 unique letters
这意味着循环有效,当我像这样运行它时:
echo "There are $(cat /usr/share/dict/dutch | grep -P '^.{14}$' | grep -vP -c '(.).*\1') words with exactly 14 unique letters" &&
echo "There are $(cat /usr/share/dict/dutch | grep -P '^.{15}$' | grep -vP -c '(.).*\1') words with exactly 15 unique letters" &&
echo "There are $(cat /usr/share/dict/dutch | grep -P '^.{16}$' | grep -vP -c '(.).*\1') words with exactly 16 unique letters"
结果是:There are 13 words with exactly 14 unique lettersThere are 2 words with exactly 15 unique lettersThere are 0 words with exactly 16 unique letters
这表明我对 grep 命令中的变量 ($i) 做错了。我不知道该怎么做或解决这个问题。
提前致谢
【问题讨论】:
-
双引号中有双引号
-
双引号中的双引号实际上是包裹在$()子shell中的,所以不会造成问题。
标签: linux variables terminal grep