How can I either strip the following character sequences from my files? ^[[64;8H, ^[[?25h, ^[[1;64r etc or avoid having expect add them in the first place?

Background:

I'm using a collection of expect scripts for certain tasks.

The output files I'm collecting often contain the above type of characters (as displayed in emacs / vi / cat -v). I've tried a number of tr commands like the following but it only makes the [64;8H etc visible.

tr -dc '[:print:]\n' < input

EDIT:

The results from above on a problematic line

[1;64r[64;1H[64;1H[2K[64;1H[?25h[64;1H[64;.....
up vote 6 down vote accepted

You were close. You want

tr -dc '[:print:]\n' <input

From the tr(1) man page:

-c, -C, --complement
use the complement of SET1

Update

If you want to remove escape sequences as well, you can use the following sed snippet:

sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"

It's taken from a serverfault question called In CentOS 4.4, how can I strip escape sequences from a text file?

  • Thanks Joseph. That makes the characters visible but doesn't strip them. I'll show the results in the question body. – Tim Brigham Dec 12 '13 at 17:16
  • @TimBrigham Does the update help? – Joseph R. Dec 12 '13 at 17:45

After some experimentation (I use the fish shell, which colourizes everything):

perl -pe '
    s/\033\\\\\[(\d+;)*\d*[[:alpha:]]//g;
    s/\033\\\\\]0;//g;
    s/\x7//g;
    s/\033\(B//g;
' expect.log

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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