简体   繁体   中英

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. 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)

I would like to scan the resulting variable (log) for backtrace sections. These sections exist between the text: "backtrace", and the following empty line.

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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