9.7 递归式地搜索文件中的文本
-R
星号(*
)通配符可以一次搜索同一目录中的多个文件,但要搜索多个子目录,就需要使用-R
(或--recursive
)选项。让我们在一组文件中搜索一下hideous这个词,这些文件都是我喜欢的19世纪和20世纪初期的恐怖小说作家的作品,虽然这些小说已经过时了,但仍然很精彩。
$ grep -R hideous *
machen/great_god_pan.txt:know, not in
your most fantastic, hideous dreams can you have
machen/great_god_pan.txt:hideously
contorted in the entire course of my practice, and I
machen/great_god_pan.txt:death was
horrible. The blackened face, the hideous form upon
lovecraft/Beyond the Wall of Sleep.txt:some hideous
but unnamed wrong, which
lovecraft/Beyond the Wall of Sleep.txt:blanket over
the hideous face, and awakened the nurse.
lovecraft/Call of Cthulhu.txt:hideous a chain. I
think that the professor, too, intended to
lovecraft/Call of Cthulhu.txt:voodoo meeting;
and so singular and hideous were the rites
lovecraft/Call of Cthulhu.txt:stated, a very
crude bas-relief of stone, comprising a hideous...
提示 当然,如果显示的结果太多,则应该通过管道把结果输出到
less
命令,如9.3节中对locate
命令的结果进行的处理一样。
$ grep -R hideous | less
另一种办法就是把命令的输出发送到一个文本文件中,然后使用任何你喜欢的文本编辑器打开:
$ grep -R hideous
> hideous_in_horror.txt
如果需要把搜索结果保存起来以备后用,这个方法不错。