是否有用于重复搜索和提取文件内容的 Perl 模块?
我需要解析一些日志文件,其中数据以特定模式重复。我需要在数据中搜索特定的“关键字”,然后从下一行中提取数据。我需要对整个文件继续此操作。 我知道这可以使用基本的 perl 脚本来完成。但是我们有没有任何 Perl 模块可以简化这种功能呢?
I need to parse some log files where the data is repetitive in a particular pattern. I need to search for particular 'keywords' in the data and then extract data from next lines. I need to continue this for the whole file.
I know this can be done using basic perl scripting. But do we have any perl module that simplifies this kind of feature?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能没有这样的模块,因为代码非常简单,而且OTOH细节非常针对特定问题。
我多次遇到过类似的问题。输入类似于:
我需要提取所有“有趣的行”,同时知道每行的日期。我认为单行文字或一次性短脚本在这方面非常成功。对于 oneliners,熟悉
-l
和-a
等有用的东西是很好的。perl -wlane '...'
这是我已经写了一千遍的东西。Probably there's no such module, because the code is quite trivial, and OTOH the details are quite problem-specific.
I've had this similar problem many times. The input has been something like:
And I've needed to extract all "interesting lines" while knowing the date for each. I think oneliners or short throwaway scripts have been very successful for the purpose. With oneliners, it's good to be familiar with useful things like
-l
and-a
.perl -wlane '...'
it's something I've written a thousand time.您可以查看
cgrep
,它就是此类处理的一个示例。它可以在管道中使用,即grep for regexp,在匹配之前输出0行,在匹配之后输出1行,然后删除原始匹配,并格式化结果。您可能不想在最后一步中使用 sed,这只是一个示例。
cgrep
出现在《Programming Perl (Camel)》一书中的最早版本中。 很容易找到。You could have a look at
cgrep
, which is an example of exactly this type of processing. It can be used in a pipeline, i.e.In other words grep for regexp, outputting 0 lines before the match and one after, then remove the original matches, and format the result. You may not want to use
sed
for the last step, it's just an example.cgrep
appears in the earliest editions of the Programming Perl (Camel) book. It's pretty easy to find.感谢您提出其他选择。
实际上,我发现使用“触发器”运算符和“if”非常恰当地解决了我的问题。仅使用此功能后,我意识到要求“模块”完成这样的琐碎任务对我来说太过分了:)。
Thanks for suggesting other options.
Actually i found that using 'flip-flop' operator with 'if' solves my problem very aptly. And after using this only i realized that asking a 'module' for a trivial task like this is too much from my side :).