I am trying to parse progress from one program (it is mkvmerge, but I hope that does not 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 the number without “Progress ” and “%”. This works:
echo -e "Progress: 0%"| sed -e 's/Progress: //' -e 's/%//' -e 's/\(....\)\(..\)\(..\)/\1-\2-\3/'
But when the Progress keeps changing, it does not work. Is there a way to do it (even without sed and with something else)?
The
\bcharacters are changing the current line so a new line never gets printed. You’re not going to have a great deal of success out of the box using a line-based stream editor like sed on that kind of input.If
mkvmergeis generating this kind of output first try looking for a switch that makes it use some other kind of progress indicator (preferrably one that prints newlines).If that doesn’t work you can try replacing the backspaces with newlines:
Add some unbuffering using
stdbufand it seems we’re there, at least for your example:The output, which appeared in the screen as it was generated thanks to
stdbuf(I trimmed some spurious newlines):