pawk-ng
awk with Python as the language. You keep awk's model (BEGIN, END,
pattern/action rules, ranges, $1/$NF, NR, NF, FS, OFS, ...) and use
plain Python instead of awk's functions: len, str methods, re, open,
your own defs.
Why it is interesting
A pawk program is the body of a loop you never write. pawk reads the input, splits every line into fields, and runs your rules once per line, so the program is only the part that changes from line to line:
what pawk runs around your code
┌───────────────────────────────────────────────────────────────────┐
│ │
│ BEGIN { ... } runs once, before any input │
│ │ │
│ ▼ │
│ ┌─ for each line of the input ─────────────────────────────┐ │
│ │ NR += 1 $0 = the line $1 $2 ... $NF │ │
│ │ │ │
│ │ pattern1 { action1 } <- your rules, in order │ │
│ │ pattern2 { action2 } │ │
│ │ ... │ │
│ └──────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ END { ... } runs once, after the last line │
│ │
└───────────────────────────────────────────────────────────────────┘
The same job, written the plain Python way and the pawk way (sum the second column of the lines where it is greater than 28):
import sys
total = 0
for line in sys.stdin: # open, loop, split, convert: all boilerplate
F = line.split()
if int(F[1]) > 28:
total += int(F[1])
print(total)
pawk '$2 > 28 { total += $2 } END { print(total) }' file
| in the plain script | in pawk |
|---|---|
for line in sys.stdin: |
implicit: your rules are the loop body |
F = line.split() |
done for you: $1, $2, $NF, NF |
int(F[1]) > 28 |
$2 > 28, fields compare as numbers when they look like numbers |
total = 0 before the loop |
not needed: unassigned names start as ""/0 |
| code after the loop | an END { ... } block |
You get awk's one-liner power with the whole of Python (comprehensions, re,
json, your own functions) inside the loop body.
Install
pip install pawk-ng
The package is pawk-ng, the command it installs is pawk. It conflicts with
the unrelated alecthomas/pawk, so do not
install both in one environment.
Examples
Fields are Python strings, so the whole language is available: methods, slicing,
re, and list/dict/generator comprehensions over F (the fields of the line).
pawk '{ print($1) }' file # first column
pawk '{ print($NF, $1) }' file # last column, then first
pawk '{ print(NR, $0) }' file # number the lines
pawk 'NR == 2' file # second line
pawk 'NR == 1, NR == 3' file # lines 1 to 3
pawk '$2 > 28' file # numeric comparison
pawk '$3 == "NY"' file # string comparison
pawk '/NY$/ && !/bob/' file # regex patterns
pawk 'NF' file # drop empty lines
pawk 'END { print(NR) }' file # count lines
pawk '{ s += $2 } END { print(s / NR) }' file # average a column
pawk '{ print(len($1), $1.upper(), $1[:2]) }' file # Python len, methods, slicing
pawk '{ $0 = re.sub("o", "0", $0); print($0) }' file # substitute with re
pawk '{ $2 = ""; print($0) }' file # blank out a column
pawk -F: '{ print($1) }' /etc/passwd # field separator
pawk -v n=2 'NR == n' file # pass a variable
pawk 'BEGIN { OFS = "," } { $1 = $1; print($0) }' file # space to comma
pawk '{ n[$3] += 1 } END { print(dict(n)) }' file # count by column
pawk 'BEGIN { print(2 ** 10) }' # no input needed
pawk '{ print([f.upper() for f in F]) }' file # list comprehension over the fields
pawk '{ print(sum(int(f) for f in F if f.is_num)) }' file # generator: sum the numeric fields
pawk '{ print({f: len(f) for f in F}) }' file # dict comprehension: field -> length
pawk '{ print("".join(f[0] for f in F)) }' file # join: first letter of each field
# dict comprehension: repeated values
pawk '{ n[$3] += 1 } END { print({k: v for k, v in n.items() if v > 1}) }' file
# collect lines, then comprehend
pawk 'BEGIN { rows = [] } { rows.append($0) } END { print([r.split()[0] for r in rows if "NY" in r]) }' file
$ and F
$n and F[n] are the same thing. Before your program runs, pawk rewrites
every $expr into F[expr], so use whichever reads better:
| awk style | Python style | meaning |
|---|---|---|
$0 |
F[0] |
the whole line |
$1, $2 |
F[1], F[2] |
a field |
$NF |
F[NF] |
the last field |
$(NF-1) |
F[NF-1] |
a computed field |
$i |
F[i] |
field number held in a variable |
$$1 |
F[F[1]] |
the field whose number is in field 1 |
F is also a regular Python object, which $ is not:
pawk '{ print(len(F)) }' file # same as NF
pawk '{ print(list(F)[1:]) }' file # fields 1..NF as a list (F[0] is not included)
pawk '{ print([f.upper() for f in F]) }' file # iterate over the fields
pawk '{ F[2] = "X"; print($0) }' file # assigning rebuilds $0, just like $2 = "X"
Things to know:
$is a text rewrite, so it only works in code, not inside strings. In an f-string writeF[1]:print(f"{F[1]:>8}").$binds tightly like in awk:$NF-1isF[NF]-1, and$1.upper()and$1[:2]areF[1].upper()andF[1][:2].- Reading a field past
NFgives"". Assigning to one extends the line. Negative numbers are an error (use$NF, not$-1). - Fields are numeric strings:
$1 > 5and$1 + $2work on numbers when the text looks like one, and$1.upper()works as a string.
Programs in a file
Rules can be mixed with ordinary Python (import, def, class). This
groups the messages of a log by severity in a dict:
BEGIN { logs = {} }
NF > 1 {
logs.setdefault($1, []).append($2)
}
END {
for severity, messages in logs.items():
print(severity, len(messages), messages)
}
pawk -f logs.awk app.log
ERROR 2 ['disk', 'timeout']
INFO 1 ['started']
WARN 1 ['slow']
Options: -F fs, -v var=value, -f progfile, -m module (from module import *).
How it differs from awk
- Python everywhere. Actions are Python statements and patterns are Python
expressions.
printis Python'sprintand must be called with parentheses. There is nolength,substr,split,sub,gsub,match,printf,getlineorsystem: use the Python equivalent. - Fields.
$1,$NF,$(NF-1),$$1are rewritten toF[1], ... (F[0]is$0). This does not apply inside strings, so writeF[1]in f-strings. Assigning$n,$0orNFrebuilds the record withOFS. - Other rewrites.
&&,||,!,/regex/(a Python regex,re.searchon$0), and barenext,nextfile,exit [n]. - Numeric strings. Fields compare and compute as numbers when they look
numeric (
$1 > $2,$1 + 1) and concatenate with plain strings. - Implicit variables. Unassigned names read as
""/0, and subscripted names (n[$3] += 1) become dicts. A name that is never assigned is aNameError. - Variables.
NR NF FNR FS OFS RS ORS FILENAME SUBSEP OFMT ENVIRON ARGV ARGC.FS/RScan be a character or a regex, andRS = ""is paragraph mode.OFSandOFMTapply when fields are assigned;ORSis only a variable (useprint(x, end=ORS)). exitinBEGINskips the input but still runsEND.
Tests
pip install -e '.[test,lint]'
python -m pytest # unit, contract and integration tests
python -m pytest -m contract
python -m pytest --cov=pawk_ng # with coverage (fails under 95%)
ruff check . # lint
The integration tests also compare against the system gawk/awk when present.
Contributing
- Fork the repository
- Create a feature branch:
git checkout -b my-new-feature - Commit your changes using Conventional Commits:
feat: add a new feature (MINOR release)fix: fix a bug (PATCH release)docs: documentation changes onlystyle: changes that don't affect code meaning (formatting, whitespace)refactor: code change that neither fixes a bug nor adds a featureperf: performance improvementstest: adding or updating testsbuild: changes to build system or dependenciesci: changes to CI configurationchore: other changes that don't modify src or test files- Adding
BREAKING CHANGE:to the footer triggers a MAJOR release
- Push to the branch:
git push origin my-new-feature - Submit a pull request
The commit messages drive the releases (see below), so the type matters: it decides whether the next version is a patch, a minor or a major one.
Semantic Versioning
This project follows Semantic Versioning.
Releases are created automatically by semantic-release
from the commit messages when commits are merged to the release branch: it
picks the next version, updates the changelog, tags the release and publishes
it to PyPI. Every push to main also publishes a development build
(<version>.devN) to TestPyPI.
License
GPL-3.0-or-later. See LICENSE.
Author
Vicente Adolfo Bolea Sanchez (@vicentebolea)
Metadata
Release files for pawk-ng 2.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pawk_ng-2.0.1.tar.gz | 63.4 kB | With provenance | Details for pawk_ng-2.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pawk_ng-2.0.1-py3-none-any.whl | Python 3 | none | any | Details for pawk_ng-2.0.1-py3-none-any.whl |
Total release size: 103.3 kB
Release files / pawk_ng-2.0.1.tar.gz
| Download URL | pawk_ng-2.0.1.tar.gz |
|---|---|
| Size | 63.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5a8646d78464723f80ef3a1fb75c8680a5cd2c898786c7c28182566d5c0f81a4
|
|
BLAKE2b-256 checksum How to use checksums |
fe5cf83bc3b8201717dc7328ad7b722e6a9e215bf1b92c6e4264593ca648556e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logRelease files / pawk_ng-2.0.1-py3-none-any.whl
| Download URL | pawk_ng-2.0.1-py3-none-any.whl |
|---|---|
| Size | 39.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4f931d7310f35ed371cdbb162bd40281989abbe06ec6cc200006ae302f8b45ed
|
|
BLAKE2b-256 checksum How to use checksums |
3fbb18d3c8b5e8c76aebb18553026fccf2c446b4b2978a4da4368babcb79e936
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logSecurity for pawk-ng 2.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for pawk_ng-2.0.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.