A simple python utility for filtering and transforming lines in bash pipes.
I created this because jq syntax is a bit hard for me to remember and awk can be annoying for JSON and other non-trivial formats.
You just write simple inline python and either return a string (to transform) or a truthy value (to filter). The line being evaluated is available as the variable "line", and the line number is available as "nline".
# Print out the "id" field of every JSON object in the file where the "enabled" field is set to a truthy value
cat my-file-full-of-json.txt \
| pype -a filter -i "json" "json.loads(line).get('enabled')" \
| pype -i "json" "json.loads(line).get('id')"
# Sleep a random amount of time for each line and print out the time slept next to the line number
# Run with 3 processes
cat my-file-full-of-json.txt \
| pype -i "time;random" "sleep_duration = random.random()*3; time.sleep(sleep_duration); ret=(nline, sleep_duration)" -P 3
curl https://gist.githubusercontent.com/turtlemonvh/4558b8bc4377b6758e289316c0141d15/raw/98eb95ecbadd067fff2cc7b18fb6ef84d9e61147/pype.py -o pype
chmod +x pype
mv pype /usr/local/bin/
Confirm setup.
$ pype -h
usage: pype [-h] [-i IMPORTS] [-a {transform,filter}] [-ns] [-P PARALLELISM]
cmd
Tool for filtering and transforming data in bash pipes using python.
positional arguments:
cmd Python code to run. Should return a string in the case
of 'transform' or a truthy object in the case of
'filter'. The current line and current line number are
available as the variables 'line' and 'nline',
respectively. Responses from multi-statement code are
supported via setting the variable 'ret'. Single
statement code does not need to explicitly set 'ret'.
optional arguments:
-h, --help show this help message and exit
-i IMPORTS, --imports IMPORTS
Imports to add, ; separated.
-a {transform,filter}, --action {transform,filter}
Type of action to take in lines. Either filter or
transform.
-ns, --strip-trailing-newlines
Set this flag to strip trailing newlines on each line.
Only relevant when calling 'transform'.
-P PARALLELISM, --parallelism PARALLELISM
The number of processes to spin up to process results.
Similar to 'xargs -P' flag. Transformed or filtered
lines are still returned in order.