2017-01-06 57 views
2

我想提取包含来自perf stat输出的'seconds time elapsed'输出的行,以查看我正在处理的某些日志脚本。从终端中的命令输出中提取单行

我不想将输出写入文件,然后搜索文件。我想用'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这些工具。我希望有人能帮助我。我也不想将输出写入文件,然后搜索文件。

回答

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%) 
+0

谢谢你,它的工作。 '2&1'是做什么的,它是什么意思? –

+0

它将stderr重定向到stdout。看到这篇文章的更多详细信息:http://stackoverflow.com/a/2342841/2679935 – julienc

1

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

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

你的不工作。我得到这个错误:“grep:经过的秒数:没有这样的文件或目录” –

+0

现在它的工作。谢谢! 2>&1做什么,它叫什么? –

+1

@SriHariVignesh Unix的标准文件描述符:https://en.wikipedia.org/wiki/File_descriptor 0 - 标准输入 1 - 标准输出 2 - stderr的 '2> $ 1' - 标准错误重定向到stdout其是管道输送到' grep' – helloV

1

这可能工作,你想要的方式:

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

输出:

0.000544399 seconds time elapsed           (+- 2.05%) 
+0

谢谢,它工作。什么是<<<'运算符? –

+0

它被称为“Here String”。请参阅:http://www.gnu.org/software/bash/manual/bashref.html#Here-Strings –