以反向管道将 gzip 文件 grep 到 tail/head 的快速方法

Fast way to grep gzipped files in reverse piped to tail/head

我需要在 gzip 文件的文件夹中搜索字符串,但是我需要从每个文件的底部开始搜索。然后我需要命令 return 当它有 10 个匹配时结果。

到目前为止我有这个:

find '/tmp/myfiles/' -type f \( -iname '*.txt' -o -iname '*.gz' \) -exec zcat -f {} \+ 2>&1 | tac | zgrep -ish 'foobar' | tail -n +1 | head -n 10

我正在使用 zcat 获取所有文件内容,然后我使用 tac 来反转它们。这比这慢得多:

find '/tmp/myfiles/' -type f \( -iname '*.txt' -o -iname '*.gz' \) -exec zgrep -ish 'foobar' {} \+ 2>&1 | tail -n +1 | head -n 10

但是这只从文件的顶部读取,我想要一种反向搜索的方法,而不必先读取整个文件...

无法避免读取文件的开头。但是为了获得最后的匹配项而反转整个文件显然比仅反转匹配项要慢得多(当然,除非每一行都包含一个匹配项)。也许试试这个;

find '/tmp/myfiles/' -type f \( -iname '*.txt' -o -iname '*.gz' \) -exec zgrep -ish 'foobar' {} \+ 2>&1 | tac | head -n 10