Perl解析大量的期望输出,然后写入文件



我正在构建一个程序,用于每48小时轮询一个网络设备,并记录用户Perl和Expect模块的输出。目前,我将输出文本保存到一个文本文件中,然后过滤文本文件以获得所需的信息。最初,这是很好的,因为在处理大小只有100 KB的文件并保留大部分文本时,效率不是那么大的问题,但是我发现了一个新的命令,我想在网络设备上远程执行。这个命令需要一个半小时来运行,创建一个大约5MB大小的文本文件,我可能只保留10KB的信息。使用基本的文件I/O(读取文本文件,写入文本文件)似乎是个坏主意,我一直在想一定有更好的方法。

到目前为止,我的研究发现:我应该用某种管道什么的。喜欢的东西:

open($filehandle, "myCommand");
There are other solutions out there, but they seem to imply that I save the ENTIRE file to one variable, when I would like to only save and modify parts of it before writing to a file. TIEHANDLE works well for overwiting "print", but I don't think that's necessary or will work here.

What I've tried so far: Redirecting STDOUT using a pipe. Result: Kept on sending the output to a file and couldn't edit the text before it was put in the file.

I have run out of things to Google. If a complete solution is too daunting of a task, a hint as to where the next steps might be would also be greatly appreciated.

Also, if the advantages in terms of processing time are actually minimal (I'm unclear on how long it takes to process a 5mb test file and will investigate), please let me know.

Perl can crunch through a 5MB file in no time, I would not worry about trying to redirect the output to a pipe when you can just open the file and parse through it once your job is done running.

    open(INPUT, "/path/to/file.log") or die "Can't open file: $!";
    while (<INPUT>) {
    chomp;
      if (/ERROR/) {
        print "Found an error!";
      }
    }
    close(INPUT);
    exit;

当你在while()循环中,你可以使用regex, split,或者任何你想在文件中找到你要找的东西。当前行存储在$_中。您将对输入文件的每行进行一次迭代。

根据您的perl版本,您可以这样做:

open(my $fh, "-|", command => @command_arguments) or die "Error: $!";
while (<$fh>) {
  # Process output of command
}
close $fh or die "Error: $!";

在旧版本(我认为是5.6或更早)中,您可以执行:

my $pid = open(my $fh, "-|");
die "Failed to fork: $!" unless defined $pid;
unless ($pid) {
  exec command => @command_arguments;
  die "Failed to exec command: $!";
}
while (<$fh>) {
  # same thing etc.
从你的帖子里我看不出你是否真的需要Expect。如果您只需要执行命令并捕获输出,那么上面的内容应该就可以了。

相关内容

  • 没有找到相关文章

最新更新