perl脚本在目录中搜索单词
我正在寻找一个perl脚本以在目录中的所有文件中grep grop for字符串。
bash命令。 代码: grep -r'word'/path/to/dir
I'am looking for a perl script to grep for a string in all files inside a directory .
bash command .
Code:grep -r 'word' /path/to/dir
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一项相当典型的任务,而我找不到最简单,最简单的简单工具的直接答案,便捷的 path :: tiny
使用
lines
在此处读取文件的方式只是一种方法。它可能不适合极大的文件,因为它一次读取所有行,其中一个更好地逐行阅读。访问
方法基于Iterator
,它在这里也干净地完成了此任务,我们必须提供一个数据结构来累积信息,但我们获得了更大的灵活性。
- f filetest 上面使用的“普通”文件,仍然有些允许;例如,它允许进行交换文件,例如一些编辑在会话期间保留的文件(例如VIM)。这些将导致各种比赛。要使用纯粹的ASCII或UTF-8文件,请使用
-t
测试。否则,会有用于递归遍历和搜索的库,例如 file :: file :: find :: find :: rule )或 path :: iterator :: ulue 。
为了完整性,这是核心 file :: find file
This is a fairly canonical task while I couldn't find straight answers with a possibly easiest and simples tool for the job, the handy Path::Tiny
The way a file is read here, using
lines
, is just one way to do that. It may not be suitable for extremely large files as it reads all lines at once, where one better read line by line.The
visit
method is based oniterator
, which accomplishes this task cleanly as wellHere we have to provide a data structure to accumulate information but we get more flexibility.
The -f filetest used above, of a "plain" file, is still somewhat permissive; it allows for swap files, for example, which some editors keep during a session (vim for instance). Those will result in all kinds of matches. To stay with purely ASCII or UTF-8 files use
-T
test.Otherwise, there are libraries for recursive traversal and searching, for example File::Find (or File::Find::Rule) or Path::Iterator::Rule.
For completeness, here is a take with the core File::Find