Analyze progress with sed (or whatever)

I'm trying to parse progress from one program (it's mkvmerge, but I hope it doesn't matter):

 echo -en "Progres: 0%" ; sleep 1 ; echo -en "\b\b25%" ; sleep 1 ; echo -en "\b\b\b50%" ; sleep 1 ; echo -en "\b\b\b75%" ; sleep 1 ; echo -e "\b\b\b100%" 

      

I would like to get a number without "Progress" and "%". It works:

echo -e "Progress: 0%"| sed -e 's/Progress: //' -e 's/%//' -e 's/\(....\)\(..\)\(..\)/\1-\2-\3/'

      

But when progress keeps changing, it doesn't work. Is there a way to do this (even without sed and with something else)?

+3


source to share


2 answers


The characters \b

change the current line, so the newline is never printed. You won't have much success out of the box using a linear stream editor like sed on this input.

If mkvmerge

generates this kind of output, first try to find a switch that allows you to use some other progress indicator (preferably one that prints newlines).

If that doesn't work, you can try replacing backspaces with new lines:

( echo -en "Progres: 0%" ; sleep 1 ; echo -en "\b\b25%" ; sleep 1 ; echo -en "\b\b\b50%" ; sleep 1 ; 
  echo -en "\b\b\b75%" ; sleep 1 ; echo -e "\b\b\b100%" ) | tr '\b' '\n'

      



Add some unbuffered using stdbuf

, and it looks like we're there, at least for your example:

( echo -en "0%" ; sleep 1 ; echo -en "\b\b25%" ; sleep 1 ; echo -en "\b\b\b50%" ; sleep 1 ; 
  echo -en "\b\b\b75%" ; sleep 1 ; echo -e "\b\b\b100%" ) | stdbuf -i0 -o0 -e0 tr '\b' '\n' | 
  stdbuf -i0 -o0 -e0 sed -e 's/Progress: //' -e 's/%//' -e 's/\(....\)\(..\)\(..\)/\1-\2-\3/'

      

The output that appeared on the screen when it was generated thanks to stdbuf

(I trimmed some spurious newline lines):

0
25
50
75
100

      

+3


source


Try shimming through tr

to include carriage returns (most likely what it uses) in the lines:

<cmd> | tr '\r' '\n' | sed ...

      



Then you can work with it split into lines as usual.

+3


source







All Articles