Extract single line from command output in terminal

294 views Asked by At

I would like to extract the line containing 'seconds time elapsed' output from perf stat output for some logging script that I am working on.

I do not want to write the output to a file and then search the file. I would like to do it using 'grep' or something similar.

Here is what I have tried:

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

For which I get

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

And I tried this

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

For which I got

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

I am new to these tools like grep, awk and sed. I hope someone can help me out. I also do not want to write the output to a file and then search the file.

3

There are 3 answers

2
julienc On BEST ANSWER

The problem here is that the output you want is sent to stderr instead of the standard output.

You can see this by redirecting stderr to /dev/null, and you'll see that the only result left is the one from the "echo" command.

~/ 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

In order to do what you want, you will have to redirect perf's stderr to the standard output, and hide the standard output. This way, perf's output will be sent to the grep command.

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

Looks like your desired output is going to stderr. Try:

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

This could work the way you intend:

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

Output:

0.000544399 seconds time elapsed                                          ( +-  2.05% )