I was trying to see if I could get pawk working. I installed it with pip install python-awk. However I can't figure out how to run it, probably under Stash.
Awk is a very useful tool and I was hoping I could get it working in Pythonista.
I was trying to see if I could get pawk working. I installed it with pip install python-awk. However I can't figure out how to run it, probably under Stash.
Awk is a very useful tool and I was hoping I could get it working in Pythonista.
So what happens when you type pawk into the StaSH commandline?
This is what my console looks like:
[~/Documents]$ pip install python-awk
Querying PyPI ...
Downloading package ...
Opening: https://files.pythonhosted.org/packages/63/2b/bd27d449ba2f102d9b2b8dfe09446abeebcd292d10ce264c4b0660155af9/python-awk-0.0.5.tar.gz
Save as: /private/var/mobile/Containers/Data/Application/7864CBEC-94B6-4103-AA16-C07C89C5C69C/tmp//python-awk-0.0.5.tar.gz (5424 bytes)
5424 [100.00%]
Extracting archive file ...
Archive extracted.
Running setup file ...
Package installed: python-awk
Installing dependency: jtutils
Querying PyPI ...
Downloading package ...
Opening: https://files.pythonhosted.org/packages/e7/66/e17c5c7cf390e4b45c2ffc2adb3ad6e575ab0ff0bc647061f43c6fb2001e/jtutils-0.0.6.tar.gz
Save as: /private/var/mobile/Containers/Data/Application/7864CBEC-94B6-4103-AA16-C07C89C5C69C/tmp//jtutils-0.0.6.tar.gz (6706 bytes)
6706 [100.00%]
Extracting archive file ...
Archive extracted.
Running setup file ...
Package installed: jtutils
Dependency available in Pythonista bundle : requests
Dependency available in Pythonista bundle : beautifulsoup4
Installing dependency: argparse
Querying PyPI ...
Downloading package ...
Opening: https://files.pythonhosted.org/packages/f2/94/3af39d34be01a24a6e65433d19e107099374224905f1e0cc6bbe1fd22a2f/argparse-1.4.0-py2.py3-none-any.whl
Save as: /private/var/mobile/Containers/Data/Application/7864CBEC-94B6-4103-AA16-C07C89C5C69C/tmp//argparse-1.4.0-py2.py3-none-any.whl (23000 bytes)
23000 [100.00%]
Installing wheel: argparse-1.4.0-py2.py3-none-any.whl...
Extracting wheel..
Extraction finished, running handlers...
Running handler 'WHEEL information checker'...
Wheel generated by: bdist_wheel (0.24.0)
Running handler 'dependency handler'...
Running handler 'top_level.txt installer'...
Copying /private/var/mobile/Containers/Data/Application/7864CBEC-94B6-4103-AA16-C07C89C5C69C/tmp/wheel_tmp/argparse-1.4.0-py2.py3-none-any.whl/argparse.py -> /private/var/mobile/Containers/Shared/AppGroup/B86CEE4E-E4A9-4BB4-9CA7-6E13BDA2C2A4/Pythonista3/Documents/site-packages-2/argparse.py
Running handler 'console_scripts installer'...
No entry_points.txt found, skipping.
Cleaning up...
Package installed: argparse
Dependency available in Pythonista bundle : six
[~/Documents]$ pawk
stash: pawk: command not found
[~/Documents]$ cd site-packages-2
[site-packages-2]$ cd pawk
[pawk]$ pawk
import: command not found
from: command not found
if: command not found
fix_broken_pipe(): command not found
pawk.pawk(): command not found
pawk is meant to be run from the command line.
Looks like maybe you need to rm pawk (in the pawk folder). I think that is being in interpreted as shell instead of python. You may need to do pawk.py or ./pawk.py or python pawk.py (I forget how much smarts is in stash)
Try editing the file pawk/pawk and add a "3" to the end of the first line:
* #!/usr/bin/env python --> #!/usr/bin/env python3
For safety's sake do the same thing to the file pawk/pawk.py.
stash does not support the #! stuff, hence why it is saying import command not found. Nor will it be able to change the interpreter version!
I tried running pawk.py in Stash in various ways but nothing produces an output (interestingly, no errors either).
However, I installed pawk in site-packages-2 using Stash while running Python v2.7. When I tried doing the install running v3.6, I got:
[~/Documents]$ pip install python-awk
FWIW a mini-version of awk.
import re
import sys
class MiniPythonAwk(object):
def __init__(self, files, FS=None):
self.FS = FS
self.FILES = files
def run(self):
# BEGIN code block starts
count = 0
# BEGIN code block ends
FS = self.FS
NR = 0
for FILEINDEX, FILENAME in enumerate(self.FILES):
FNR = 0
fp = open(FILENAME)
for SS in fp:
NR += 1
FNR += 1
if self.FS:
S = SS.rstrip().split(self.FS)
else:
S = SS.split()
NF = len(S)
# ACTION code block starts
if re.search('ff', SS) or (S[1] == '5') :
count += 1
print("{}:{}:".format(FILENAME, NR), SS.rstrip())
# ACTION code block ends
fp.close()
# END code block starts
print("Count:", count)
# END code block ends
if __name__ == '__main__':
files = sys.argv[1:]
if len(files):
MiniPythonAwk(files).run()
'''
test_input1.txt
1 2 3
4 5 6
7 8 9
test_input2.txt
aa bb cc
dd ee ff
gg hh ii
python mini_python_awk.py test_input1.txt test_input2.txt
test_input1.txt:2: 4 5 6
test_input2.txt:5: dd ee ff
Count: 2
'''
@ihf https://github.com/alecthomas/pawk/blob/master/setup.py#L10-L12 is clear that Python 2 is no longer supported.
Maybe open an issue at https://github.com/alecthomas/pawk/issues