使用grep等bash工具解决难题

时间:2021-02-21 19:10:04

I need to solve a puzzle using shell script. I tried to combine grep with rev and saved the output into a temporary text file but still don't know how to solve it entirely.

我需要使用shell脚本解决难题。我尝试将grep与rev组合并将输出保存到临时文本文件中,但仍然不知道如何完全解决它。

That's the puzzle to solve :

这就是要解决的难题:

j s e t f l
a l s f e l
g a a n p l
e p f d p k
r e g e l a
f n e t e n

The file that contains the wordlist to use is in http://pastebin.com/DP4mFZAr

包含要使用的词表的文件位于http://pastebin.com/DP4mFZAr中

I know how to tell grep where to find the patterns to match as fixed strings extracted from a text file using $ grep -Ff wordlist puzzle and how to search for mirrored words using $ rev puzzle | grep -Ff wordlist puzzle , thus dealing with the horizontal lines, but how do I deal with vertical words too ?

我知道如何告诉grep在哪里找到匹配的模式作为使用$ grep -Ff wordlist拼图从文本文件中提取的固定字符串以及如何使用$ rev puzzle搜索镜像单词grep -Ff wordlist拼图,因此处理水平线,但我如何处理垂直字呢?

1 个解决方案

#1


18  

I am covering horizontal and vertical matching. The main idea is to remove the spaces and then use grep -f with the given list of words, stored in words file.

我正在覆盖水平和垂直匹配。主要思想是删除空格,然后将grep -f与给定的单词列表一起使用,存储在单词文件中。

With grep -f, the results are shown within the line. If you just want to see the matched test, use grep -of.

使用grep -f,结果显示在行内。如果您只想查看匹配的测试,请使用grep -of。

Horizontal matching

$ cat puzzle | tr -d ' ' | grep -f words
alsfel
gaanpl
regela
fneten

$ cat puzzle | tr -d ' ' | grep -of words
als
gaan
regel
eten

Vertical matching

For this, we firstly have to transpose the content of the file. For this, I use what I used for another answer of mine:

为此,我们首先必须转置文件的内容。为此,我使用我用于另一个答案的方法:

transpose () {
  awk '{for (i=1; i<=NF; i++) a[i,NR]=$i; max=(max<NF?NF:max)}
        END {for (i=1; i<=max; i++)
              {for (j=1; j<=NR; j++) 
                  printf "%s%s", a[i,j], (j<NR?OFS:ORS)
              }
        }'
}

And let's see:

让我们看看:

$ cat puzzle | transpose | tr -d ' ' | grep -f words
jagerf
slapen
esafge
tfndet
lllkan

$ cat puzzle | transpose | tr -d ' ' | grep -of words
jager
slapen
af
ge
de
kan

You can then use rev (as you suggest in your question) for mirrored words. Also tac can be interesting for vertically mirrored words.

然后,您可以使用rev(如您在问题中所建议的)镜像词。对于垂直镜像的单词,tac也很有趣。

Diagonal matching

For the diagonal matching, I think that an interesting approach would be to move every single line a little bit to the left/right. This way,

对于对角线匹配,我认为一种有趣的方法是将每一条线向左/右移动一点点。这条路,

e x x x x
x g x x x
x x g x x

can become

可以变成

e x x x x
g x x x
g x x

and you can use the vertical/horizontal approaches.

你可以使用垂直/水平方法。

For this, you can use printf as described in Using variables in printf format:

为此,您可以使用printf格式中使用变量中所述的printf:

$ cat a
e x x x x
x g x x x
x x g x x
$ awk -v c=20 '{printf "%*s\n", c, $0; c-=2}' a
        e x x x x
        x g x x x
    x x g x x

#1


18  

I am covering horizontal and vertical matching. The main idea is to remove the spaces and then use grep -f with the given list of words, stored in words file.

我正在覆盖水平和垂直匹配。主要思想是删除空格,然后将grep -f与给定的单词列表一起使用,存储在单词文件中。

With grep -f, the results are shown within the line. If you just want to see the matched test, use grep -of.

使用grep -f,结果显示在行内。如果您只想查看匹配的测试,请使用grep -of。

Horizontal matching

$ cat puzzle | tr -d ' ' | grep -f words
alsfel
gaanpl
regela
fneten

$ cat puzzle | tr -d ' ' | grep -of words
als
gaan
regel
eten

Vertical matching

For this, we firstly have to transpose the content of the file. For this, I use what I used for another answer of mine:

为此,我们首先必须转置文件的内容。为此,我使用我用于另一个答案的方法:

transpose () {
  awk '{for (i=1; i<=NF; i++) a[i,NR]=$i; max=(max<NF?NF:max)}
        END {for (i=1; i<=max; i++)
              {for (j=1; j<=NR; j++) 
                  printf "%s%s", a[i,j], (j<NR?OFS:ORS)
              }
        }'
}

And let's see:

让我们看看:

$ cat puzzle | transpose | tr -d ' ' | grep -f words
jagerf
slapen
esafge
tfndet
lllkan

$ cat puzzle | transpose | tr -d ' ' | grep -of words
jager
slapen
af
ge
de
kan

You can then use rev (as you suggest in your question) for mirrored words. Also tac can be interesting for vertically mirrored words.

然后,您可以使用rev(如您在问题中所建议的)镜像词。对于垂直镜像的单词,tac也很有趣。

Diagonal matching

For the diagonal matching, I think that an interesting approach would be to move every single line a little bit to the left/right. This way,

对于对角线匹配,我认为一种有趣的方法是将每一条线向左/右移动一点点。这条路,

e x x x x
x g x x x
x x g x x

can become

可以变成

e x x x x
g x x x
g x x

and you can use the vertical/horizontal approaches.

你可以使用垂直/水平方法。

For this, you can use printf as described in Using variables in printf format:

为此,您可以使用printf格式中使用变量中所述的printf:

$ cat a
e x x x x
x g x x x
x x g x x
$ awk -v c=20 '{printf "%*s\n", c, $0; c-=2}' a
        e x x x x
        x g x x x
    x x g x x