从终端的命令输出中提取单行

时间:2017-01-06 21:06:04

标签: regex bash grep perf

我想提取包含'秒时间的行'我正在处理的一些日志记录脚本的perf stat输出的输出。

我不想将输出写入文件然后搜索文件。我想用' grep'或类似的东西。

以下是我的尝试:

perf stat -r 10 echo "Sample_String" | grep -eE "seconds time elapsed"

我得到了

    grep: seconds time elapsed: No such file or directory
    echo: Broken pipe
    echo: Broken pipe
    echo: Broken pipe 
    echo: Broken pipe
    echo: Broken pipe
    echo: Broken pipe
    echo: Broken pipe
    echo: Broken pipe
    echo: Broken pipe

    Performance counter stats for 'echo Sample_String' (10 runs):

      0.254533      task-clock (msec)         #    0.556 CPUs utilized            ( +-  0.98% )
             0      context-switches          #    0.000 K/sec                  
             0      cpu-migrations            #    0.000 K/sec                  
            56      page-faults               #    0.220 M/sec                    ( +-  0.53% )
       <not supported>      cycles                   
       <not supported>      stalled-cycles-frontend  
       <not supported>      stalled-cycles-backend   
       <not supported>      instructions             
       <not supported>      branches                 
       <not supported>      branch-misses            

   0.000457686 seconds time elapsed                                          ( +-  1.08% )

我试过这个

perf stat -r 10 echo "Sample_String" > grep -eE "seconds time elapsed"

我得到了

 Performance counter stats for 'echo Sample_String -eE seconds time elapsed' (10 runs):

      0.262585      task-clock (msec)         #    0.576 CPUs utilized            ( +-  1.11% )
             0      context-switches          #    0.000 K/sec                  
             0      cpu-migrations            #    0.000 K/sec                  
            56      page-faults               #    0.214 M/sec                    ( +-  0.36% )
    <not supported>      cycles                   
    <not supported>      stalled-cycles-frontend  
    <not supported>      stalled-cycles-backend   
    <not supported>      instructions             
    <not supported>      branches                 
    <not supported>      branch-misses            

   0.000456035 seconds time elapsed                                          ( +-  1.05% )

我是grep,awk和sed这些工具的新手。我希望有人可以帮助我。我也不想将输出写入文件然后搜索文件。

3 个答案:

答案 0 :(得分:1)

这里的问题是您想要的输出被发送到stderr而不是标准输出。

您可以通过将stderr重定向到/ dev / null来看到这一点,并且您将看到剩下的唯一结果是&#34; echo&#34;命令。

~/ perf stat -r 10 echo "Sample_String" 2>/dev/null
Sample_String
Sample_String
Sample_String
Sample_String
Sample_String
Sample_String
Sample_String
Sample_String
Sample_String
Sample_String

为了做你想做的事,你必须将perf的stderr重定向到标准输出,并隐藏标准输出。这样,perf的输出将被发送到grep命令。

~/ perf stat -r 10 echo "Sample_String" 2>&1 >/dev/null | grep 'seconds time elapsed'       
       0,013137361 seconds time elapsed                                          ( +- 96,08% )

答案 1 :(得分:1)

看起来您想要的输出将转到stderr。尝试:

perf stat -r 10 echo "Sample_String" 2>&1 | grep "seconds time elapsed"

答案 2 :(得分:1)

这可能会按照您的意图运作:

grep -e "seconds time elapsed" <<< "$(perf stat -r 10 echo "Sample_String" 2>&1 >/dev/null)"

输出:

0.000544399 seconds time elapsed                                          ( +-  2.05% )