duanhuayong6687 2017-09-03 18:05
浏览 837

grep来自命令输出的特定字符串

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.

  • 写回答

1条回答

  • douwen7331 2017-09-03 18:32
    关注

    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
    )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧