在Bash中用重复的字母查找字谜

时间:2017-05-18 13:27:06

标签: linux bash

我试图在bash中找到字谜。

我尝试了下面的代码,它假设给我结果" steep",但它无效。

grep -P '^(?:([eepst])(?!.*?\1)){5}$' /usr/share/dict/words
不过,这是一个学校作业的问题。有一个提示说' cut'可能有用。

1 个答案:

答案 0 :(得分:0)

假设这个例子:

$ cat /usr/share/dict/words
step
steep
stop

这些命令的行为类似于:

# grep all combinations of the letters in bracket on line with exactly 5 chars
$ grep -Ei  "^[epst]{5}$" /usr/share/dict/words
steep

# grep all combinations of the letters in bracket on line with exactly 4 chars
$grep -Ei "^[epst]{4}$" /usr/share/dict/words
step

# grep all combination of the letters in bracket on line with exactly 4 chars
$grep -Ei "^[opst]{4}$" /usr/share/dict/words
stop

# grep all combinations of the letters in bracket on line with exactly 4 chars
$grep -Ei "^[eopst]{4}$" /usr/share/dict/words
step
stop

# grep all combinations of the letters in bracket on line with exactly 1 to 5 chars
$grep -Ei "^[epst]{1,5}$" /usr/share/dict/words
step
steep

如果文件/usr/share/dict/words的结构不是每行一个单词,而是多个结构:

steep|rising or falling sharply; almost perpendicular.

这可能是提及cut小费的原因

实施可能是:

grep -Ei  "^[epst]{,5}\|" /usr/share/dict/words | cut -d'|' -f1