{"id":13937703,"url":"https://github.com/alecthomas/pawk","last_synced_at":"2025-04-12T14:57:10.714Z","repository":{"id":6867925,"uuid":"8116879","full_name":"alecthomas/pawk","owner":"alecthomas","description":"PAWK - A Python line processor (like AWK)","archived":false,"fork":false,"pushed_at":"2024-02-12T18:06:45.000Z","size":37,"stargazers_count":516,"open_issues_count":3,"forks_count":33,"subscribers_count":23,"default_branch":"master","last_synced_at":"2024-10-10T16:06:15.096Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alecthomas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-02-09T23:48:22.000Z","updated_at":"2024-09-23T14:08:03.000Z","dependencies_parsed_at":"2024-10-25T17:07:03.645Z","dependency_job_id":"89d76f9b-4204-4c04-9e0c-e7742956275f","html_url":"https://github.com/alecthomas/pawk","commit_stats":{"total_commits":36,"total_committers":9,"mean_commits":4.0,"dds":0.25,"last_synced_commit":"d60f78399e8a01857ebd73415a00e7eb424043ab"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fpawk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fpawk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fpawk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fpawk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alecthomas","download_url":"https://codeload.github.com/alecthomas/pawk/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248586249,"owners_count":21128997,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-07T23:03:47.751Z","updated_at":"2025-04-12T14:57:10.689Z","avatar_url":"https://github.com/alecthomas.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# PAWK - A Python line processor (like AWK)\n\nPAWK aims to bring the full power of Python to AWK-like line-processing.\n\nHere are some quick examples to show some of the advantages of pawk over AWK.\n\nThe first example transforms `/etc/hosts` into a JSON map of host to IP:\n\n\tcat /etc/hosts | pawk -B 'd={}' -E 'json.dumps(d)' '!/^#/ d[f[1]] = f[0]'\n\nBreaking this down:\n\n1. `-B 'd={}'` is a begin statement initializing a dictionary, executed once before processing begins.\n2. `-E 'json.dumps(d)'` is an end statement expression, producing the JSON representation of the dictionary `d`.\n3. `!/^#/` tells pawk to match any line *not* beginning with `#`.\n4. `d[f[1]] = f[0]` adds a dictionary entry where the key is the second field in the line (the first hostname) and the value is the first field (the IP address).\n\nAnd another example showing how to bzip2-compress + base64-encode a file:\n\n\tcat pawk.py | pawk -E 'base64.encodestring(bz2.compress(t))'\n\n### AWK example translations\n\nMost basic AWK constructs are available. You can find more idiomatic examples below in the example section, but here are a bunch of awk commands and their equivalent pawk commands to get started with:\n\nPrint lines matching a pattern:\n\n\tls -l / | awk '/etc/'\n\tls -l / | pawk '/etc/'\n\nPrint lines *not* matching a pattern:\n\n\tls -l / | awk '!/etc/'\n\tls -l / | pawk '!/etc/'\n\nField slicing and dicing (here pawk wins because of Python's array slicing):\n\n\tls -l / | awk '/etc/ {print $5, $6, $7, $8, $9}'\n\tls -l / | pawk '/etc/ f[4:]'\n\nBegin and end end actions (in this case, summing the sizes of all files):\n\n\tls -l | awk 'BEGIN {c = 0} {c += $5} END {print c}'\n\tls -l | pawk -B 'c = 0' -E 'c' 'c += int(f[4])'\n\nPrint files where a field matches a numeric expression (in this case where files are \u003e 1024 bytes):\n\n\tls -l | awk '$5 \u003e 1024'\n\tls -l | pawk 'int(f[4]) \u003e 1024'\n\nMatching a single field (any filename with \"t\" in it):\n\n\tls -l | awk '$NF ~/t/'\n\tls -l | pawk '\"t\" in f[-1]'\n\n## Installation\n\nIt should be as simple as:\n\n```\npip install pawk\n```\n\nBut if that doesn't work, just download the `pawk.py`, make it executable, and place it somewhere in your path.\n\n## Expression evaluation\n\nPAWK evaluates a Python expression or statement against each line in stdin. The following variables are available in local context:\n\n- `line` - Current line text, including newline.\n- `l` - Current line text, excluding newline.\n- `n` - The current 1-based line number.\n- `f` - Fields of the line (split by the field separator `-F`).\n- `nf` - Number of fields in this line.\n- `m` - Tuple of match regular expression capture groups, if any.\n\n\nIn the context of the `-E` block:\n\n- `t` - The entire input text up to the current cursor position.\n\nIf the flag `-H, --header` is provided, each field in the first row of the input will be treated as field variable names in subsequent rows. The header is not output. For example, given the input:\n\n```\ncount name\n12 bob\n34 fred\n```\n\nWe could do:\n\n```\n$ pawk -H '\"%s is %s\" % (name, count)' \u003c input.txt\nbob is 12\nfred is 34\n```\n\nTo output a header as well, use `-B`:\n\n```\n$ pawk -H -B '\"name is count\"' '\"%s is %s\" % (name, count)' \u003c input.txt\nname is count\nbob is 12\nfred is 34\n```\n\nModule references will be automatically imported if possible. Additionally, the `--import \u003cmodule\u003e[,\u003cmodule\u003e,...]` flag can be used to import symbols from a set of modules into the evaluation context.\n\neg. `--import os.path` will import all symbols from `os.path`, such as `os.path.isfile()`, into the context.\n\n## Output\n\n### Line actions\n\nThe type of the evaluated expression determines how output is displayed:\n\n- `tuple` or `list`: the elements are converted to strings and joined with the output delimiter (`-O`).\n- `None` or `False`: nothing is output for that line.\n- `True`: the original line is output.\n- Any other value is converted to a string.\n\n### Start/end blocks\n\nThe rules are the same as for line actions with one difference.  Because there is no \"line\" that corresponds to them, an expression returning True is ignored.\n\n\t$ echo -ne 'foo\\nbar' | pawk -E t\n    foo\n    bar\n\n\n## Command-line usage\n\n```\nUsage: cat input | pawk [\u003coptions\u003e] \u003cexpr\u003e\n\nA Python line-processor (like awk).\n\nSee https://github.com/alecthomas/pawk for details. Based on\nhttp://code.activestate.com/recipes/437932/.\n\nOptions:\n  -h, --help            show this help message and exit\n  -I \u003cfilename\u003e, --in_place=\u003cfilename\u003e\n                        modify given input file in-place\n  -i \u003cmodules\u003e, --import=\u003cmodules\u003e\n                        comma-separated list of modules to \"from x import *\"\n                        from\n  -F \u003cdelim\u003e            input delimiter\n  -O \u003cdelim\u003e            output delimiter\n  -L \u003cdelim\u003e            output line separator\n  -B \u003cstatement\u003e, --begin=\u003cstatement\u003e\n                        begin statement\n  -E \u003cstatement\u003e, --end=\u003cstatement\u003e\n                        end statement\n  -s, --statement       DEPRECATED. retained for backward compatibility\n  -H, --header          use first row as field variable names in subsequent\n                        rows\n  --strict              abort on exceptions\n```\n\n## Examples\n\n### Line processing\n\nPrint the name and size of every file from stdin:\n\n\tfind . -type f | pawk 'f[0], os.stat(f[0]).st_size'\n\n\u003e **Note:** this example also shows how pawk automatically imports referenced modules, in this case `os`.\n\nPrint the sum size of all files from stdin:\n\n\tfind . -type f | \\\n\t\tpawk \\\n\t\t\t--begin 'c=0' \\\n\t\t\t--end c \\\n\t\t\t'c += os.stat(f[0]).st_size'\n\nShort-flag version:\n\n\tfind . -type f | pawk -B c=0 -E c 'c += os.stat(f[0]).st_size'\n\n\n### Whole-file processing\n\nIf you do not provide a line expression, but do provide an end statement, pawk will accumulate each line, and the entire file's text will be available in the end statement as `t`. This is useful for operations on entire files, like the following example of converting a file from markdown to HTML:\n\n\tcat README.md | \\\n\t\tpawk --end 'markdown.markdown(t)'\n\nShort-flag version:\n\n\tcat README.md | pawk -E 'markdown.markdown(t)'\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falecthomas%2Fpawk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falecthomas%2Fpawk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falecthomas%2Fpawk/lists"}