Filtering terminal commands on linux

A

Adriaan Renting

I have the problem that I need to interact with a CD/DVD burning program
called gear. I do this by running it in a pseudo terminal.

I also need to log what I'm doing, so I copy all output I get from gear
to a logfile. The problem is that gear uses terminal control commands to
create a nice commandline interface.
I can filter out all non printable characters to get something I can put
in my logfile, but this leaves a lot of the terminal control commands.
My problem is that the control sequences are variable in length, and
themselves printable text. they probably vary depending on the $TERM
setting too. Does anyone have code that can parse this, so I can get rid
of the ugly control sequences?

Any suggestions on how to do this with regular expressions is welcome
too, with my limited knowledge I don't know how to do it. Finding the
beginning is kind of easy, as it always starts with <unprinteable
control character>[, but the length varies after that.

My output now looks like this:
2005-07-28 14:27:58 Message : [76C^M Scanning busses for recorder
devices...please wait...
2005-07-28 14:27:59 Message : [K[77C^M Driver found: Linux Scsi
Generic v3 Driver.
2005-07-28 14:27:59 Message : [76C^M PLEXTOR DVDR PX-716A
revision 1.07 found on /dev/sg1
2005-07-28 14:27:59 Message :
2005-07-28 14:27:59 Message : [76C^M [23;54H[25DPLEXTOR DVDR
PX-716A [11;78H^M Recorder PLEXTOR DVDR PX-716A used.
2005-07-28 14:27:59 Message :
[9B[K[79C[6DReady^M[K[79C[18DInitializing tape^M[K[79C [25DInitializing
tape driver[12;78H^M Selected tape interface: Standard SCSI driver
2005-07-28 14:27:59 Message : [76C^M Scanning bus for tape
units...please wait...^M [K[77C^M Driver found: Linux Scsi Generic v3
Driver.
2005-07-28 14:27:59 Message : [76C^M HP[7CC5683A[11Crevision C908
found on /dev/sg0

I would like:
2005-07-28 14:27:58 Message : Scanning busses for recorder
devices...please wait...
2005-07-28 14:27:59 Message : Driver found: Linux Scsi Generic v3
Driver.
2005-07-28 14:27:59 Message : PLEXTOR DVDR PX-716A revision
1.07 found on /dev/sg1
2005-07-28 14:27:59 Message :
2005-07-28 14:27:59 Message : PLEXTOR DVDR PX-716A Recorder
PLEXTOR DVDR PX-716A used.
2005-07-28 14:27:59 Message : Ready Initializing tape Initializing
tape driver Selected tape interface: Standard SCSI driver
2005-07-28 14:27:59 Message : Scanning bus for tape units...please
wait... Driver found: Linux Scsi Generic v3 Driver.
2005-07-28 14:27:59 Message : HP 5683A revision 08 found on /dev/sg0

Thank you.
 
L

Lonnie Princehouse

Firstly, there's probably a better way to do whatever you're trying to
do w.r.t cd/dvd burning. I'm not familiar with gear, but its webpage
lists "Batch file scripting capability" as a feature, which suggests
that you might be able to do what you want without parsing output
intended for humans. There are also a multitude of command-line cd and
dvd utilities for Linux which might be better for scripting.

That said, it shouldn't be too hard to craft a regular expression that
matches ANSI control sequences. Using
http://www.dee.ufcg.edu.br/~rrbrandt/tools/ansi.html as a reference,
here's how to do this for the first few control sequences...

esc = '\x1B'
start_control_sequence = esc + '['

Pn = r'\d+' # Numeric parameter
Ps = '%s(;%s)*' % (Pn,Pn) # Selective parameter
PL = Pn
Pc = Pn

control_sequences = [
PL + ';' + Pc + '[Hf]', # Cursor position
Pn + '[ABCD]', # Cursor up|down|forward|backward
's', # Save cursor position
'u', # Restore cursor position
'2J', # Erase display
'K', # Erase line
Ps + 'm', # Set graphics mode
'=' + Pn + '[hl]', # Set|Reset mode
# ... etc
]

match_ansi = re.compile(start_control_sequence +
'(' + '|'.join(control_sequences) + ')')

def strip_ansi(text):
return match_ansi.sub('',text)


(note: code is untested.. may contain typos)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top