A command line utility for transforming text using Python expressions. If you know Python, you already know pystr.
echo "Hello World" | pystr 's.lower().replace(" ", "-")'
hello-worldOr let Claude write the Python for you.
echo "5550123456" | pystr --prompt "format as US phone number: 000-000-0000"
555-012-3456pystr is a single ~250 line Python script. Copy it or download it from github:
curl -fsSL https://raw.githubusercontent.com/armandmcqueen/pystr/main/install.sh | bashRequires uv.
For prompt mode (-p), set your ANTHROPIC_API_KEY environment variable.
pystr reads from stdin and runs your Python expression on line, printing the expression result.
| Variable | Description |
|---|---|
s |
The current line (string) |
i |
Line number (0-indexed) |
math |
The math module |
re |
The re module for regex |
All Python builtins (len, int, str, sum, sorted, etc.) are available.
It's trivial to add dependencies to the script if you need - add it manually or open an issue.
echo "hello world" | pystr 's.upper()'
HELLO WORLD
seq 5 | pystr 'int(s) ** 2'
1
4
9
16
25
cat file.txt | pystr 'f"{i}: {s}"' # number linesPrint lines where the expression is truthy.
seq 10 | pystr -g 'int(s) % 2 == 0'
2
4
6
8
10
cat log.txt | pystr -g '"ERROR" in s' # find error linesInstead of line-by-line, s becomes the entire input.
cat file.txt | pystr -a 'len(s.splitlines())' # count lines
cat file.txt | pystr -a '",".join(s.splitlines())' # join linesDon't want to write the expression? Describe what you want in plain English.
echo "5550123456" | pystr -p "format as US phone number"
555-012-3456
echo "john,doe,30" | pystr -p "get the second comma-separated field"
doeUse --show to see the generated code:
echo "hello world" | pystr -p --show "reverse each word"
Generated code: ' '.join(word[::-1] for word in s.split())
olleh dlrowUses Claude Haiku by default (--model sonnet or --model opus for smarter models). Haiku is fast, but you may need to be descriptive. Sonnet is good if you don't want to think.
Flags can be combined. For example, grep + prompt:
cat uuids.txt | pystr -g -p "invalid uuids"
not-a-uuid
hello worldpystr [OPTIONS] EXPRESSION
Options:
-a, --all Read all input at once (s = entire input)
-g, --grep Filter mode: print lines where expression is truthy
-n, --no-print Don't auto-print (for expressions with side effects)
-p, --prompt Interpret expression as natural language
--show Show generated code (with -p)
--confirm Ask before executing generated code (with -p)
--model MODEL haiku (default), sonnet, or opus (with -p)
--dry-run Show the LLM prompt without executing (with -p)
-v, --version Show version
Apache 2.0
