Extract single line from command output in terminal

Sri Hari Vignesh

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.

julienc

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Launch Pycharm from command line (terminal)

Single Line sftp from Terminal

how to push to git from Terminal / Command Line

BATCH - How to get only a single line from a multi-line command output

How to extract (read and delete) a line from file with a single command?

How to extract a single word from the output of a Linux command?

Extract row from dataframe and output as a single line in R

How to extract .msi features from command line?

Extract specific strings from a single long line

Getting specific line from terminal output

Writing a single line command that interprets conditional output from pdbedit

Command line: Extract substring from output

How can I split a PDF file into single pages quickly (i.e. from the Terminal command line)?

Strange output from terminal `exit` command is this a virus?

Extracting columns from multiple files into a single output file from the command line

How to extract the first column from an output line?

How to search and extract string from command output?

Mathematics on output from command line

How to output a text's first line and last line to the terminal as a single command

Extract number from the command line output by batch file

Formatting command output that is a long single line

C - Output the longest line from terminal input

Output data from subprocess command line by line

Command line bash how to extract json text from a curl output of a webpage

extract output from command and pipe it on the command

How can you pass a multi-line output from one command as n arguments to a single command, not n commands with a single argument?

Change mac terminal settings from the command line

Extract first n bytes from .tar.gz and output as a .tar.gz in a single command

Open Windows Terminal PREVIEW from command line