Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

tse is an input stream editor in Python.

Project description

tse processes text input stream with Python expressions. Like AWK, tse command line option is a series of pair of condition and action:

tse -c COND1 -a ACTION1 -c COND2 -a ACTION2 -a ACTION3

or

tse -s COND1 ACTION1 -s COND2 ACTION2 ACTION3

For example, to find lines starts with ‘abc’

tse -a "^abc" -s "print L"

to find line contains URL

tse -a "http://\\S+" "print S0"  -a "mailto://\\S+" -s "print S0"

to convert upper case

tse -a ".*" -s "print L.upper()"
usage: tse [-h] [--statement PATTERN [ACTION ...]] [--pattern PATTERN]
           [--action ACTION] [--begin BEGIN] [--end END]
           [--input-encoding INPUT_ENCODING]
           [--output-encoding OUTPUT_ENCODING] [--script-file SCRIPT_FILE]
           [--module MODULE] [--module-star MODULE_STAR]
           [FILE [FILE ...]]

Text Stream Editor in Python

positional arguments:
  FILE                  With no FILE, or when FILE is -, read standard input.

optional arguments:
  -h, --help            show this help message and exit
  --statement PATTERN [ACTION ...], -s PATTERN [ACTION ...]
                        a pair of pattern and action(s).
  --pattern PATTERN, -p PATTERN
                        pattern for trailing action(s)
  --action ACTION, -a ACTION
                        action to be executed.
  --begin BEGIN, -b BEGIN
                        action invoked before input files have been read.
  --end END, -e END     action invoked after input files have been exhausted.
  --input-encoding INPUT_ENCODING, -ie INPUT_ENCODING
                        encoding of input stream.
  --output-encoding OUTPUT_ENCODING, -oe OUTPUT_ENCODING
                        encoding of output stream.
  --script-file SCRIPT_FILE, -F SCRIPT_FILE
                        specifies an alternative script file. the default
                        script file is ~/.tserc.
  --module MODULE, -m MODULE
                        module to be imported.
  --module-star MODULE_STAR, -ms MODULE_STAR
                        module to be imported in form of "from modname import
                        *".

Variables

Following variables can be used within action statement.

sys, os, path, re:
 These modules are imported by default.
FILENAME:The name of file currently reading.
LINENO:Line numberof the current line.
L:Current line.
S:Part of Text matched to condition regex.
S0, S1, …:sub-string matched to condition regex. S0 is entire matched part, S1, S2 are sub group of condition regex.
(name):If condition regex has group names defined by ‘(?P<name>)’, sub-string could be referenced by variable ‘name’.
M:Match object

Script file

If the file ~/.tserc exists, the file is execused at begging. You can import your faivorite modules, or write convenient functions you like. The values defined in the scipt file are accessible by action specifyed by command options.

Examples

Print sum of numeric characters in an each line of input stream:

tse -a "\d+" "print(sum(int(s) for s in re.findall(r"\d+", L)))" \
     -a "=" "print('done'); sys.exit(0)"

Sum all numeric characters in all lines:

tse -b "all=0" \
     -a "\d+" "all+=sum(int(s) for s in re.findall(r"\d+", L)))" \
     -e "=" "print(all); sys.exit(0)"

Find all extention parts in current directory:

find . | tse -a ".*" "print path.splitext(L)[1]"

Project details


Release history Release notifications

History Node

0.0.15

History Node

0.0.14

History Node

0.0.13

History Node

0.0.12

History Node

0.0.11

History Node

0.0.10

History Node

0.0.9

History Node

0.0.8

History Node

0.0.7

History Node

0.0.6

History Node

0.0.5

History Node

0.0.4

History Node

0.0.3

This version
History Node

0.0.2

History Node

0.0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
tse-0.0.2.tar.gz (5.6 kB) Copy SHA256 hash SHA256 Source None Feb 11, 2012

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page