【问题标题】:Extract single line from command output in terminal从终端的命令输出中提取单行
【发布时间】:2017-01-07 05:57:31
【问题描述】:

我想从我正在处理的某些日志记录脚本的 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 等工具。我希望有人可以帮助我。我也不想将输出写入文件然后搜索文件。

【问题讨论】:

    标签: regex bash grep perf


    【解决方案1】:

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

    您可以通过将 stderr 重定向到 /dev/null 来看到这一点,您会看到唯一剩下的结果就是来自“echo”命令的结果。

    ~/ 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的标准错误重定向到标准输出,并隐藏标准输出。这样,perf 的输出将被发送到grep 命令。

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

    【讨论】:

    • 谢谢,它成功了。 '2>&1' 是什么意思,是什么意思?
    • 它将标准错误重定向到标准输出。有关详细信息,请参阅此帖子:stackoverflow.com/a/2342841/2679935
    【解决方案2】:

    看起来你想要的输出是stderr。试试:

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

    【讨论】:

    • 你的不工作。我收到此错误:“grep: seconds time elapsed: No such file or directory”
    • 现在可以了。谢谢! 2>&1 有什么作用,叫什么?
    • @SriHariVignesh Unix 标准文件描述符:en.wikipedia.org/wiki/File_descriptor 0 - stdin 1 - stdout 2 - stderr 2&gt;$1 - 将 stderr 重定向到通过管道传输到 grep 的 stdout
    【解决方案3】:

    这可以按照您的意愿工作:

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

    输出:

    0.000544399 seconds time elapsed                                          ( +-  2.05% )
    

    【讨论】:

    猜你喜欢
    • 2013-09-06
    • 2019-01-06
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 2013-02-01
    • 1970-01-01
    相关资源
    最近更新 更多