Package: py3line
Author:Pahaz WhiteRepo:https://github.com/pahaz/py3line/Pyline is a UNIX command-line tool for bash one-liner scripts.
It’s python line alternative togrep,sed, andawk.This project inspired by:pyfil,piep,pysed,pyline,pypandJacob+Mark recipeWHY I MAKE IT?Sometimes, I have to usesed/awk/grep. Usually for simple text
processing. Find some pattern inside the text file using Python regexp,
or comment/uncomment some config line by bash one line command.I always forget the necessary options andsed/awkDSL.
But I now python, I like it, and I want use it for this simple bash tasks.
Defaultpython -cis not enough to write readable bash one-liners.Why not apyline?Don`t support python3Have many optionsDon`t support command chainingPRINCIPLESAS MUCH SIMPLE TO UNDERSTAND BASH ONE LINER SCRIPT AS POSSIBLELESS SCRIPT ARGUMENTSAS MUCH EASY TO INSTALL AS POSSIBLE (CONTAINER FRIENDLY ???)SMALL CODEBASE (less 500 loc)LAZY AND EFFECTIVE AS POSSIBLEInstallationpy3lineis on PyPI, so simply run:pip install py3lineorsudo curl -L "https://61-63976011-gh.circle-artifacts.com/0/py3line-$(uname -s)-$(uname -m)" -o /usr/local/bin/py3line
sudo chmod +x /usr/local/bin/py3lineto have it installed in your environment.For installing from source, clone therepoand run:python setup.py installTutorialLets start with examples, we want to evaluate a number of words in each line:$echo-e"Here are\nsome\nwords for you."|./py3line.py"x = len(line.split(' ')); print(x, line)"2Hereare1some3wordsforyou.Py3line process input stream by python code line by line.echo -e “Here arensomenwords for you.”– create an input stream consists of three lines|– pipeline input stream to py3line“x = len(line.split()); print(x, line)”– define 2 actions: “x = len(line.split(’ ‘))” evaluate number of words in each line, then “print(x, line)” print the result. Each action apply to the input stream step by step.The example above can be represented as the following python code:import sys

def process(stream):
    for line in stream:
        x = len(line.split(' '))  # action 1
        print(x, line)            # action 2
        yield line

stream = (line.rstrip("\r\n") for line in sys.stdin if line)
stream = process(stream)
for line in stream: passYou can also get the executed python code by--pycodeargument.$./py3line.py"x = len(line.split(' ')); print(x, line)"--pycode#skipbashtest...Stream transformLets try more complex example, we want to to evaluate the number of words in the whole file.
This value is easy to calculate if you convert the input stream from a stream of lines
to a number of words in line stream. Just overridelinevariable$ echo -e "Here are\nsome\nwords for you." | ./py3line.py "line = len(line.split()); print(sum(stream))"
6Here we have a stream transformation action“print(sum(stream))”.The example above can be represented as the following python code:import sys

def process(stram):
    for line in stream:
        line = len(line.split())  # action 1
        yield line

def transform(stream):
    print(sum(stream))            # action 2
    return stream

stream = (line.rstrip("\r\n") for line in sys.stdin if line)
stream = transform(process(stream))
for line in stream: passYou can also get the executed python code by--pycodeargument.$./py3line.py"line = len(line.split()); print(sum(stream))"--pycode#skipbashtest...Lazy as possiblePy3line does calculations only when necessary by the use of python generators.
This means that the input stream does not fit into memory and you can easy process more data than your RAM allows.But it also imposes limitations on the ability to work with the data flow.
You cannot use multiple aggregation functions at the same time. For example,
if we want to calculate the maximum number of words in a line and the total number of words in a whole file at the same time.:$ echo -e "Here are\nsome\nwords for you." | ./py3line.py "line = len(line.split()); print(sum(stream)); print(max(stream))"  #skipbashtest
6
2019-05-05 14:55:09,353 | ERROR   | Traceback (most recent call last):
  File "<string>", line 15, in <module>
    stream = transform2(process1(stream))
  File "<string>", line 10, in transform2
    print(max(stream))
ValueError: max() arg is an empty sequenceWe can see theempty sequenceerror. It throws because ourstreamgenerator is already empty.
And we can’t find any max value on empty stream.stream memorizationWe can solve it by converting thestreamgenerator to a list of values in memory using pythonlist(stream)function.$ echo -e "Here are\nsome\nwords for you." | ./py3line.py "line = len(line.split()); stream = list(stream); print(sum(stream), max(stream))"
6 3The example above can be represented as the following python code:import sys

def process(stram):
    for line in stream:
        line = len(line.split())     # action 1
        yield line

def transform(stream):
    stream = list(stream)            # action 2
    print(sum(stream), max(stream))  # action 3
    return stream

stream = (line.rstrip("\r\n") for line in sys.stdin if line)
stream = transform(process(stream))
for line in stream: passevaluate on the flyWe can also solve it without putting the stream into memory. Just use the auxiliary variables where
we will place the calculated result in the process of processing the stream.$ echo -e "Here are\nsome\nwords for you." | ./py3line.py "s = 0; m = 0; num_of_words = len(line.split()); s += num_of_words; m = max(m, num_of_words); print(s, m)"
2 2
3 2
6 3The example above can be represented as the following python code:import sys

def process(stram):
    s = 0                                 # action 1
    m = 0                                 # action 2
    for line in stream:
        num_of_words = len(line.split())  # action 3
        s += num_of_words                 # action 4
        m = max(m, num_of_words)          # action 5
        print(s, m)                       # action 6
        yield line

stream = (line.rstrip("\r\n") for line in sys.stdin if line)
stream = process(stream)
for line in stream: passBut we want only the last result. We don’t want to see intermediate results.
To do this, you can add a loop over all elements of the stream before printing
byfor line in stream: pass. Don’t worry, this loop doesn’t add unnecessary calculations
as we use Python language generators. The loop will simply force the stream
to be iterated before the print function called.$ echo -e "Here are\nsome\nwords for you." | ./py3line.py "s = 0; m = 0; num_of_words = len(line.split()); s += num_of_words; m = max(m, num_of_words); for line in stream: pass; print(s, m)"
6 3The example above can be represented as the following python code:import sys

def process(stram):
    global s, m
    s = 0                                 # action 1
    m = 0                                 # action 2
    for line in stream:
        num_of_words = len(line.split())  # action 3
        s += num_of_words                 # action 4
        m = max(m, num_of_words)          # action 5
        yield line

def transform(stream):
    global s, m
    for line in stream: pass              # action 6
    print(s, m)                           # action 7
    return stream

stream = (line.rstrip("\r\n") for line in sys.stdin if line)
stream = transform(process(stream))
for line in stream: passpython generator lazinessLet’s check python generator laziness.
Just runfor line in stream: print(1);twice in a row:$ echo -e "Here are\nsome\nwords for you." | ./py3line.py "for line in stream: print(1); for line in stream: print(1)"
1
1
1As we can see, it only one-time iteration over the python generator items.
And all subsequent iterations will work with an empty generator,
which is equivalent to a cycle through an empty list.The example above can be represented as the following python code:import sys

def transform(stream):
    for line in stream: pass              # action 1 (3 iterations)
    for line in stream: pass              # action 2 (0 iterations)
    return stream

stream = (line.rstrip("\r\n") for line in sys.stdin if line)
stream = transform(stream)
for line in stream: pass                  # (0 iterations)work with a part of streamTODO ….DetailsLet us define some terminology.py3line “action1; action2; action3We have actions: action1, action2 and action3.
Each action have type. It may beelement processingorstream transformation.We can understand the type of action based on the variables used in it.
We have two variables:lineandstream.
They are markers that define the type of action.Lets look at some types from examples abow:x = line.split()                 -- element processing
print(x, line)                   -- element processing
print(sum(stream))               -- stream transformation
stream = list(stream)            -- stream transformation
print(sum(stream), max(stream))  -- stream transformation
s = 0                            -- unidentified
m = 0                            -- unidentified
num_of_words = len(line.split()) -- element processing
s += num_of_words                -- unidentified
m = max(m, num_of_words)         -- unidentified
print(s, m)                      -- unidentified
for line in stream: pass         -- stream transformation[rule1]If an action has an undefined type, it inherits its type from the previous action.[rule2]If there is no previous action, then the action is considered a stream transformation.Examples:s = 0                            -- stream transformation (because of [rule2])
num_of_words = len(line.split()) -- element processing (because of `line` marker)
s += num_of_words                -- element processing (because of [rule1])
print(s)                         -- element processing (because of [rule1])And if we want to doprintat the and, we should have somestreammarker in actions before.s = 0                            -- stream transformation (because of [rule2])
num_of_words = len(line.split()) -- element processing (because of `line` marker)
s += num_of_words                -- element processing (because of [rule1])
stream                           -- stream transformation (because of `stream` marker)
print(s)                         -- stream transformation (because of [rule1])Unfortunately, it is not so clearly to people who are not familiar with the the implementation.
Therefore, it is better to use a more explicit to readers actions likefor line in stream: pass.s = 0                            -- stream transformation (because of [rule2])
num_of_words = len(line.split()) -- element processing (because of `line` marker)
s += num_of_words                -- element processing (because of [rule1])
for line in stream: pass         -- stream transformation (because of `stream` marker)
print(s)                         -- stream transformation (because of [rule1])Some examples# Print every line (null transform)$cat./testsuit/test.txt|./py3line.py"print(line)"Thisismycat,whosenameisBetty.Thisismydog,whosenameisFrank.Thisismyfish,whosenameisGeorge.Thisismygoat,whosenameisAdam.# Number every line$cat./testsuit/test.txt|./py3line.py"stream = enumerate(stream); print(line)"(0,'This is my cat,')(1,' whose name is Betty.')(2,'This is my dog,')(3,' whose name is Frank.')(4,'This is my fish,')(5,' whose name is George.')(6,'This is my goat,')(7,' whose name is Adam.')# Number every line$cat./testsuit/test.txt|./py3line.py"stream = enumerate(stream); print(line[0], line[1])"0Thisismycat,1whosenameisBetty.2Thisismydog,3whosenameisFrank.4Thisismyfish,5whosenameisGeorge.6Thisismygoat,7whosenameisAdam.Or justcat ./testsuit/test.txt | ./py3line.py "stream = enumerate(stream);print(*line)"# Print every first and last word$cat./testsuit/test.txt|./py3line.py"s = line.split(); print(s[0], s[-1])"Thiscat,whoseBetty.Thisdog,whoseFrank.Thisfish,whoseGeorge.Thisgoat,whoseAdam.# Split into words and print as list (strip al non word char like comma, dot, etc)$cat./testsuit/test.txt|./py3line.py"print(re.findall(r'\w+', line))"['This','is','my','cat']['whose','name','is','Betty']['This','is','my','dog']['whose','name','is','Frank']['This','is','my','fish']['whose','name','is','George']['This','is','my','goat']['whose','name','is','Adam']# Split into words (strip al non word char like comma, dot, etc)$cat./testsuit/test.txt|./py3line.py"print(*re.findall(r'\w+', line))"ThisismycatwhosenameisBettyThisismydogwhosenameisFrankThisismyfishwhosenameisGeorgeThisismygoatwhosenameisAdam# Find all three letter words$cat./testsuit/test.txt|./py3line.py"print(re.findall(r'\b\w\w\w\b', line))"['cat'][]['dog'][][][][][]# Find all three letter words + skip empty listscat./testsuit/test.txt|./py3line.py"line = re.findall(r'\b\w\w\w\b', line); if not line: continue; print(line)"['cat']['dog']# Regex matching with groups$cat./testsuit/test.txt|./py3line.py"line = re.findall(r' is ([A-Z]\w*)', line); if not line: continue; print(*line)"BettyFrankGeorgeAdam# cat ./testsuit/test.txt | ./py3line.py "line = re.search(r' is ([A-Z]\w*)', line); if not line: continue; line.group(1)"$cat./testsuit/test.txt|./py3line.py"rgx = re.compile(r' is ([A-Z]\w*)'); line = rgx.search(line); if not line: continue; print(line.group(1))"BettyFrankGeorgeAdam# head -n 2
# cat ./testsuit/test.txt | ./py3line.py "stream = enumerate(stream); if line[0] >= 2: break; print(line[1])"$cat./testsuit/test.txt|./py3line.py"stream = list(stream)[:2]; print(line)"Thisismycat,whosenameisBetty.# Print just the URLs in the access log$cat./testsuit/nginx.log|./py3line.py"print(shlex.split(line)[13])"HEAD/HTTP/1.0HEAD/HTTP/1.0HEAD/HTTP/1.0HEAD/HTTP/1.0HEAD/HTTP/1.0GET/admin/moktoring/session/add/HTTP/1.1GET/admin/jsi18n/HTTP/1.1GET/static/admin/img/icon-calendar.svgHTTP/1.1GET/static/admin/img/icon-clock.svgHTTP/1.1HEAD/HTTP/1.0HEAD/HTTP/1.0HEAD/HTTP/1.0HEAD/HTTP/1.0HEAD/HTTP/1.0GET/logout/?reason=startApplicationHTTP/1.1GET/HTTP/1.1GET/login/?next=/HTTP/1.1POST/admin/customauth/user/?q=%D0%9F%D0%B0%D1%81%D0%B5%D1%87%D0%BD%D0%B8%D0%BAHTTP/1.1# Print most common accessed urls and filter accessed more then 5 times$cat./testsuit/nginx.log|./py3line.py"line = shlex.split(line)[13]; stream = collections.Counter(stream).most_common(); if line[1] < 5: continue; print(line)"('HEAD / HTTP/1.0',10)Complex examples# create directory treeecho-e"y1\nx2\nz3"|./py3line.py"pathlib.Path('/DATA/' + line +'/db-backup/').mkdir(parents=True, exist_ok=True)"groupby3lines...(https://askubuntu.com/questions/1052622/separate-log-text-according-to-paragraph)HELP$ ./py3line.py --help
usage: py3line.py [-h] [-v] [-q] [--version] [--pycode]
                  [expression [expression ...]]

Py3line is a UNIX command-line tool for a simple text stream processing by the
Python one-liner scripts. Like grep, sed and awk.

positional arguments:
  expression     python comma separated expressions

optional arguments:
  -h, --help     show this help message and exit
  -v, --verbose
  -q, --quiet
  --version      print the version string
  --pycode       show generated python code$ ./py3line.py --version
0.3.1