A simple variant of AWK in Python.
Keep simple, wonderful and the most frequently used features in awk, while replacing the complicated and ambiguous syntax with Python. As a result, we can get the power of both Python and awk by using pyawk, a pythonic awk.
-
usage
pyawk:
pyawk [option] 'pattern { action }' fileawk:
awk [option] 'pattern { action }' file1 file2 ... -
option
pyawk awk Remarks -F fs -F fs -f progfile -f progfile (1) -v (verbose) -v var=val × other options in awk(1) In
pyawk, "progfile" is actually a Python module, in which all commands are included in a list calledcmds. While "progfile" is just a normal text file inawk. -
pattern
pyawk awk Remarks /regex/ <=> $0/regex/ /regex/ <=> $0 ~ /regex/ (1) $1/regex/ $1 ~ /regex/ (1) $1!/regex/ $1 !~ /regex/ (1) all valid expressions in Pythonall valid expressions in awk(2) begpat, endpat begpat, endpat (3) BEGIN { action } BEGIN { action } (4) END { action } END { action } (4) no pattern <=> match every record no pattern <=> match every record (5) × other patterns in awk(1) regular expression
(2) expression
(3) begpat, endpat
(4) BEGIN/END
(5) empty
-
action
pyawk awk all valid statements in Pythonall valid statements in awkprint($1, $2, $3) print $1, $2, $3 no action <=> print($0)no action <=> print $0print(×, must beprint($0))print(√, <=>print $0)× variables used before defined -
built-in variable
pyawk awk Remarks NR NR Number of Records NF NF Number of Fields FS FS input Field Separator RS RS input Record Separator OFS OFS Output Field Separator ORS ORS Output Record Separator × other built-in variables in awk
sudo python setup.py install
sudo python setup.py uninstall
See help message of command pyawk:
pyawk 'END { print(NR) }' /etc/passwd
echo -e 'python .py\nperl .pl\nshell .sh' | pyawk '$2!/pl/{ print($1) }'
ps aux | pyawk 'BEGIN { print("USER|PID|COMMAND") }; $2 == 10, $2 == 50 { print($1, $2, $11) }'
See directory sample:
pyawk -f sample.cmds sample/data.txt