2017-01-06 58 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 –