Arnt Gulbrandsen
About meAbout this blog

Dropping terminal escape sequences

I need to reconfigure a device via its serial console; the device emits many ANSI/VT escape sequences and other control codes. These are perhaps helpful if one is typing, but not so helpful when a script is to reset and reconfigure the device.

The following perl s/// magic gets rid of most (but not all) escape codes.

s/\e[@-Z\\-~]/ /gs; s/\e\[[0-9;]*[A-Za-z]/\n/gs; s/[\r\n]+/\n/gs;

The first regular expression removes single-byte escape sequences (ESC O for example), the second removes the very common ESC [ sequences (cursor movement, colours and so on), and the last line cleans up when the first two have generated too much whitespace.

Why does the first regexp turn the input into a space, and the second into a linefeed? No good reason. The output I see seems to make a little more sense that way.

(Ob-rant: Today, I actually feel sympathetic about devices that have to be configured via embedded web servers.)