5

I'm trying to pipe the output of a script (Mocha) to another script. However there is one problem: Mocha generates quite a few ansi escape characters to update the screen on the fly. These characters are also send through the pipe.

Is there a way to process the ansi sequence such that the output is the same as the final output to the screen? I do want to keep color escape sequences, but not the curser movement escapes.

Edit: I have a partial solution now (for Mocha only): so far it seems that Mocha with the spec output (the one I use) only generates color ecape characters and the CSI 0G escape sequence. The CSI 0G escape character means that the cursor should move back to the beginning of the line. Mocha uses this to overwrite a line completely. Therefore you could simply create a sed regexp which will delete everything up to that escape sequence on a line: sed 's/^.*\x1b\[0G//g'. I am still looking for the complete solution though.

5
  • The same question exists here stackoverflow.com/questions/6306728/…
    – dset0x
    Commented Oct 17, 2012 at 20:50
  • @zmode that's not the identical, that one is about removing all ansi codes, whereas here the OP wants to "keep color escape sequences"
    – eis
    Commented Oct 17, 2012 at 20:55
  • @Tiddo Do you know how the other script would handle the color escapes? it would sound peculiar that it could handle them...
    – eis
    Commented Oct 17, 2012 at 21:02
  • @eis - The other script is ansifilter. It generates colored HTML from ansi input. By design it is able to handle color escapes, but for some reason it can't handle the movement escape characters.
    – Tiddo
    Commented Oct 17, 2012 at 21:36
  • @Tiddo ok, makes sense.
    – eis
    Commented Oct 18, 2012 at 8:24

2 Answers 2

1
sed -e 's/\x1b\[[0-9?]\+[^m0-9?]//g'

should remove all escape codes that are not "Select Graphic Rendition" codes — things like color, bold, italics.

Edit: You should also use your sed 's/^.*\x1b\[0G//g' filter before mine if you want the previous text to actually be overwritten. Otherwise both the previous text and the new text will be output. If you want all of the sequences to be carried out so that the output is the same as what would be displayed in a terminal emulator, you'd have to make some sort of background terminal emulator that could actually execute all the codes, and at that point, such a project would eclipse ansifilter.

-1

This is an ancient question now,but I think the answer would have been to use unbuffer which is packaged with expect.

apt get install expect

unbuffer [command] | less -r
unbuffer [command] > [file]
unbuffer [command] | tee [file]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .