Skip to main content

red

Python regex command-line tool, to replace functionality akin to 'perl -ne'.

Installation

From the project root directory::

$ python setup.py install

Usage

Use --help/-h to view info on the arguments::

$ red --help

The special variables available:

:line: for the entire line that was matched, without the trailing newline :g: for groups, which you index to get that match group, as g[0] in "(\w+) ." :ag: for aggregated groups with the -a option, which is a list of all match groups :d: for the groupdict, which you can index by named group as d["foo"] in "(?P\w+) ." :ad: for the aggregated group dicts, a list of dicts

Example usage::

$ cat test.txt 
foo 1 bar 2
fiz 5 baz 10
funk 10 bunk 9
funk a bunk b
a b c d
aaaaa
bbbb
cc

Use it like grep::

$ cat test.txt | red "\w+ (\d+) \w+ (\d+)" 
foo 1 bar 2
fiz 5 baz 10
funk 10 bunk 9

It works with path as argument as well, but the path must appear directly after the regex (oddity of argparse).::

$ red "\w+ (\d+) \w+ (\d+)" test.txt

Use it to evaluate Python code on groups stored in variable g::

$ red "\w+ (\d+) \w+ (\d+)" test.txt -e "int(g[0]) + int(g[1])"
3
15
19

Import an arbitrary library and do absolutely anything::

$ red '\w+ (\d+) \w+ (\d+)' test.txt -i json -e '"{} => {}".format(line, json.dumps(g))'
foo 1 bar 2 => ["1", "2"]
fiz 5 baz 10 => ["5", "10"]
funk 10 bunk 9 => ["10", "9"]

If you want to execute a few python statements before the eval, you can do that as well with -x::

$ red "(\w+) (\d+).*" test.txt -x 'x = int(g[1]) ; y = g[0][::-1]' -e '(x, y)'
(1, 'oof')
(5, 'zif')
(10, 'knuf')

Use it to aggregate across all of stdin, into list ag::

$ cat test.txt | red "\w+ (\d+) \w+ (\d+)" -a "sum([int(x[0]) for x in ag])"
16

Aggregate has its own -X for an exec before the aggregate too::

$ red "(\w+) (\d+).*" test.txt -X 's = sum(int(g[1]) for g in ag)' -a 's'
16

Evaluate on each match, and aggregate against all matches::

$ cat test.txt | red "\w+ (\d+) \w+ (\d+)" -a "sum([int(x[0]) for x in ag])" -e "'adding {}'.format(g[0])"
adding 1
adding 5
adding 10
16

You can use named groups as well, stored in variables d and aggregated into ad::

$ cat test.txt | red "\w+ (?P<first>\d+) \w+ \d+" -e "'first value is {first}'.format(**d)"
first value is 1
first value is 5
first value is 10

Even multiline will work, since whatever is passed into -x is just exec'd::

$ red '.*' test.txt -x '
# if you want multiline, just hit apostrophe and press enter
# and start typing
if line.startswith("foo"):
     print("Line started with foo: {}".format(line))
'

Line started with foo: foo 1 bar 2

Get creative!::

$ cat urls.txt 
https://www.google.com/
http://www.yahoo.com/
http://www.example.com/foo

$ red "(.*)" urls.txt -i requests -x 'response = requests.get(line)' -e '[response.status_code, response.content[:20]]'
[404, '<!doctype html>\n<htm']
[200, '<!doctype html><html']
[200, '<?xml version="1.0" ']

$ cat somelogfile.log 
INFO: visited http://www.example.com/foo
ERROR: visited https://www.google.com/
ERROR: visited http://www.yahoo.com/

$ cat somelogfile.log | red '(?P<debuglevel>ERROR|INFO): \S+ (?P<url>.*)' -i requests,collections -x 'response = requests.get(d["url"])' -e '(response.status_code, response.content[0:20])' -a 'collections.Counter([d["debuglevel"] for d in ad]).items()'
(404, '<!doctype html>\n<htm')
(200, '<!doctype html><html')
(200, '<?xml version="1.0" ')
[('INFO', 1), ('ERROR', 2)]

Disclaimer

Code in -e and -a are run through eval, so if you paste in code from someone, it will run. Just sayin'. Only run code you trust, or have inspected personally. The same goes for any library you import with -i.

Release Notes

:0.2.1: Added -x for pre-eval exec, added -i for importing libraries, and -X for aggregate exec :0.2.0: Added -i/--import functionality :0.1.1: Path works as CLI arg :0.1.0: Version is available on pypi, with functionality of evaluation and aggregation :0.0.1: Project created

Metadata

Release files for red 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for red 0.2.3
File Size Uploaded
red-0.2.3.tar.gz 3.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for red 0.2.3
File Interpreter ABI Platform
red-0.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 7.4 kB

Release files / red-0.2.3.tar.gz

Download URL red-0.2.3.tar.gz
Size 3.2 kB
Tags Source
SHA-256 checksum
How to use checksums
189a96cdfcaee5893c4fe033e5dec021bf6fa6a34a1afd02a3bfd25c08b4a26e
BLAKE2b-256 checksum
How to use checksums
8193906b76fa9d614269887803d2a765cf6746e6cc98f678e3aa34bb2d791cb2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / red-0.2.3-py3-none-any.whl

Download URL red-0.2.3-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
91c177936b73238ae8d2fbe75319d0052bf11e629786a6c0c43ba0b6b857f8ce
BLAKE2b-256 checksum
How to use checksums
3b60338d0c2591e76e66b44bae2d552f49cbb63ff47a45475aa8bdf87ad11c44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page