是否有用于重复搜索和提取文件内容的 Perl 模块?

发布于 2024-12-15 00:54:54 字数 126 浏览 0 评论 0原文

我需要解析一些日志文件,其中数据以特定模式重复。我需要在数据中搜索特定的“关键字”,然后从下一行中提取数据。我需要对整个文件继续此操作。 我知道这可以使用基本的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

许你一世情深 2024-12-22 00:54:54

可能没有这样的模块,因为代码非常简单,而且OTOH细节非常针对特定问题

我多次遇到过类似的问题。输入类似于:

Date: 2011-11-10
<an interesting line>
<another interesting line>
Date: 2011-11-11
<more interesting lines>

我需要提取所有“有趣的行”,同时知道每行的日期。我认为单行文字或一次性短脚本在这方面非常成功。对于 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:

Date: 2011-11-10
<an interesting line>
<another interesting line>
Date: 2011-11-11
<more interesting lines>

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.

梦醒灬来后我 2024-12-22 00:54:54

您可以查看cgrep,它就是此类处理的一个示例。它可以在管道中使用,即

cat mylog | cgrep -w0:1 'regexp' | grep -v 'regexp' | sed 's/.../.../'

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.

cat mylog | cgrep -w0:1 'regexp' | grep -v 'regexp' | sed 's/.../.../'

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.

霞映澄塘 2024-12-22 00:54:54

感谢您提出其他选择。
实际上,我发现使用“触发器”运算符和“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 :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文