473,750 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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...pleas e 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...pleas e 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 2143
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_s equence = esc + '['

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

control_sequenc es = [
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(star t_control_seque nce +
'(' + '|'.join(contro l_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
10856
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 included a code snippet from Chuck Blake 'ls' app in python. It seems to do the job just fine on my comp, but regrettably, I'm not sassy enough to wrap my head around the fine grain details on this one. How cross-platform is this? Is there a more...
4
2008
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 on specified coordinates? And also: is it possible to override, from python code, something on a
5
2007
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 spent hours reading other posts regarding the advantages and disadvantages upgrading my mdb (jet engine) to an sql BE. I am the system engineer, administrator and programmer of our company's database. Our mdb FE is very complex, using hundreds of...
16
2717
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 names of Linux packages to install. Running the script as above will create this line: sudo aptitude install package1 package2 ...
4
2107
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 below, then start the program with Application(), then click the Quit button, my terminal never prints anything again (such as commands I type).
5
21874
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 commands which I use to send an SMS to my email via Motorola 9505A. An error occurred after I press <Ctrl+Z> to execute the SMS. The message that I want to sent is: - "megat@geoid.biz testing from 9505A" Header of the message (pdu format) is: -
3
3602
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 Linux 9 which is installed on my system. But when it comes to Linux terminal, I know not much about it. Q1. Which one is easy to learn-linux or unix terminal commands?
2
7288
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, i'm pretty sure its running php, because i can then type code, however, it doesn't interpret the code until i press CTRL+D (which quits php) does anyone know what keystroke or how to just interpret the typed code without logging out for some...
4
3206
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 window from already opened terminal window. Can this be achieved.If yes,please provide a tested solution Thanks
0
8999
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9575
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9394
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6803
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4712
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.