2

I'm trying to post-process the output of script into a more readable form, similar to Removing control chars (including console codes / colours) from script output, but I've noticed that col doesn't always work.

For instance,

$ cat -v uncolored 
foo^H^H^Hbfoo^H^H^Hafoo^H^H^Hr^M
$ col -bp < uncolored
baroo

Why doesn't col -bp output just bar? Where are the extra two os coming from?

  • Is your terminal's backspace character ^M or ^?? – DopeGhoti Jun 11 '18 at 22:40
  • The backspace character is ^H. – Jeffrey Jun 11 '18 at 23:49
2

^H in this case is backspace, AKA dec/hex 8 or oct 10 or \b. All it is doing is moving the cursor; take this example:

$ printf 'bravo\10\10X'
braXo

We have moved the cursor back 2, but we only wrote over one letter, the v. We didn't write over the o, so it remains. If you want to get rid of the rest of the letters, you have to overwrite them with something, usually a space character:

$ printf 'bravo\10\10X '
braX

http://wikipedia.org/wiki/Backspace#%5eH

  • Ah, the uncolor script from the other post removes all the ^[[K controls which would've cleared the rest of the line, except col doesn't understand those either. – Jeffrey Jun 11 '18 at 23:48
0

Here's a hacky workaround:

sed -re ':b; s,[^\x08]\x08,,g; tb'

  • :b: Label b
  • s,[^\x08]\x08,,g: Pair a non-backspace character with a backspace character and remove both
  • tb: If the previous s directive did something, jump back to label b

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.