Search code examples
unixgrepprogress-barpipecurses

Stationary text output from command line program, using grep (curses?)


I have a command line program that will periodically print statements, such as "5% done", along with other extraneous statements. I would like to pipe this output into grep, use it to select the progress bar lines, and display them all in one line (ie rewrite the line in place). ie, write:

commandlinecall | grep '%' ???

and get this sequence of output over time:

 25% done

.

 50% done

.

 75% done

.

 100% done

rather than this:

25% done

.

25% done
50% done

.

25% done
50% done
75% done

.

25% done
50% done
75% done
100% done

Solution

  • Not sure how to do this with grep, but awk can easily do it. For example:

    # Simple script to generate completion strings
    i=0
    while :; do 
      echo $((i+=4))% done
      sleep $((RANDOM%3))
    done |
    
    # Look for '%' and print out the line with a carriage-return prepended 
    awk '/%/ { printf "\r%s Done", $1 }'
    

    Copy paste the above to a bash command line to test.

    You might also consider piping the progress-percent to zenity. For example:

    # Simple script to generate completion strings
    i=0
    while :; do 
      echo $((i+=4))% done
      sleep $((RANDOM%3))
    done |
    
    # This assumes that the progress strings are formatted as above
    grep --line-buffered -o '^[^%]*' |
    
    # zenity quits when 100 arrives on stdin
    zenity --progress --auto-close --no-cancel