Zeljko
Zeljko

Reputation: 43

grep specific string from command output

I analyze stream from multicast address. Output of the command looks like this:

...
Sep 03 19:06:45: INFO: Bitrate: 8241 Kbit/s
Sep 03 19:06:45: ERROR: Scrambled: 250=4792 251=132 252=132 253=263
...

I need get value after Bitrate: [ 8241 ] and set frist variable in php script, and after Scrambled: only before "=" [250,251,252,253] for second variable. Example $var1=8241; $var2=250,251,252,253. I found how grep -oP '(?<=Bitrate: )[0-9]+' so I get "8241" but i need both variables i one step.

Upvotes: 0

Views: 351

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92894

It would be problematic for you to grep the needed values into the 2 variables simultaneously.

So here's a complete php solution (using shell_exec function to call awk script):

<?php

$command = 'yourcommand | awk \'/Bitrate/{ printf "%d ", $6 }/Scrambled/{ for(i=6;i<=NF;i++)' 
                           . 'printf "%d ",substr($i,1,index($i,"=")) }\' 
';
$result = trim(shell_exec($command));
if ($result) {
    $arr = explode(" ", $result);
    $bitrate = $arr[0];
    $scrambled = array_slice($arr, 1);
}

print_r('bitrate: ' . $bitrate . PHP_EOL);
print_r('scrambled: ');
print_r($scrambled);

Formatted output:

bitrate: 8241
scrambled: Array
(
    [0] => 250
    [1] => 251
    [2] => 252
    [3] => 253
)

Upvotes: 1

Related Questions