Snippet: A sed shell in Python

Posted by Kiyoshi Murata Fri, 21 May 2010 07:17:00 GMT

Here is a very simple shell in Python to test regular expressions with sed.

The shell works by matching the input as a sed command against a given data.
Each input line is evaluated as a Python expression (and it must evaluate to a [base]string) and then sent to sed as a command to be matched against this given data.
So every input is the same as:

echo <data> | sed -r -e <input>

<data> is set by using the d() function:

>> d("My data")
>> r's#^My#Our#'
Our data

To see what <data> contains, simply use the d() function without arguments:

>> d()
My data

(<data> defaults to the alphabet string "abc...z")

To exit, use Ctrl-C (KeyboardInterrupt) or send an EOF to it (Windows: Ctrl-Z+Enter, Linux: Ctrl-D) or yet use the exit() function.