473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Filtering terminal commands on linux

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.
Jul 29 '05 #1
1 2124
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)

Jul 29 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Joel Hedlund | last post by:
Hi all! I use python for writing terminal applications and I have been bothered by how hard it seems to be to determine the terminal size. What is the best way of doing this? At the end I've...
4
by: fivestars | last post by:
Hi there. I'm computer science student at the end of my degree. I'm new about python. I've a question for all of you. Do you know how to write, from python code, on a unix(linux) terminal...
5
by: cityrock | last post by:
Hello friends. It has come time for me to face a dilema i have been thinking about for a long time, but actually doing nothing. Now its time to act. The situation is "simple", and i have just...
16
by: John Salerno | last post by:
Here's my new project: I want to write a little script that I can type at the terminal like this: $ scriptname package1 where scriptname is my module name and any subsequent arguments are the...
4
by: Chris | last post by:
Hi, I'm puzzled by some strange behavior when my Python/Tkinter application quits (on linux): the terminal from which I started Python is messed up. If start up python, then import the code...
5
by: McGuard | last post by:
I am trying to send an SMS using hyper terminal in windows XP pro. I am connecting my Satellite Phone (Motorola Iridium 9505A) to my notebook via serial cable (RS232) . Below are the sample...
3
AmberJain
by: AmberJain | last post by:
HELLO, I have some queries. Please help. Let me tell you experts that I'm a linux newbie and an ABSOLUTE UNIX illiterate person. I know a bit of linux and can easily handle GUI of Red hat...
2
by: misterparker | last post by:
Hey so I'm not new to php, but I'm no guru either, however recently (6 months) i have gotten a macbook, and have been playing around on the terminal, come to find if you type "php" in the terminal,...
4
by: gaurav kashyap | last post by:
Dear all, I am using Microsoft Windows XP.Using putty.exe,I connected to LINUX server and a terminal window gets opened.Here i logeed in as root. What i want to do is open another terminal...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.