简体   繁体   English

如何将触发器操作器应用于cat输出?

[英]How do apply the flip-flop operator to cat output?

I'm parsing a crash log using perl and I want to extract the backtrace component. 我正在使用perl解析崩溃日志,我想提取回溯组件。 I obtain the log file using the following command: 我使用以下命令获取日志文件:

$log = `adb shell 'ls -d ./tombstones/*' |grep tombstone_ | tr '\r' ' ' | tail -n1 | xargs adb shell cat`;

(I'm not familiar with perl, as you can see) (我不熟悉perl,你可以看到)

I would like to scan the resulting variable (log) for backtrace sections. 我想扫描结果变量(日志)的回溯部分。 These sections exist between the text: "backtrace", and the following empty line. 这些部分存在于文本:“backtrace”和以下空行之间。

My question is, how do I apply the flip flop operator to the local variable as if it were a file input? 我的问题是,如何将触发器操作符应用于局部变量,就像它是文件输入一样?

Do you need to use the flip-flop operator? 你需要使用触发器操作器吗? How about a regular expression? 正则表达怎么样?

@backtrace_sections = $log =~ /(^backtrace.*?)\n\n/gm;

I assume what you want is an equivalent of the construct 我假设你想要的是构造的等价物

while (<>) {
    if (m/backtrace/ .. m/^$/) {
        # processing
    }
}

I see two ways to do this off the top of my head: 我认为有两种方法可以做到这一点:

  1. Use the backtick operator in array context: 在数组上下文中使用反引号运算符:

     my @lines = qx{$your_command}; for (@lines) { if (m/backtrace/ .. m/^$/) { # process } } 
  2. Use open to open the file: 使用open打开文件:

     open my $fh, '-|', qq{$your_command} or die "Can't open command: $!"; while (<$fh>) { if (m/backtrace/ .. m/^$/) { # process } } close $fh or die "close failed: $! $?"; 

    Doing it this way has the nice effect that you don't have to read the entire output into memory. 这样做会产生很好的效果,您不必将整个输出读入内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM