4 ms·
If you wanted to do this with python: cat beef.txt | grep "lasagna" | sort -n | uniq The correct way would seem to be to write four python programs, named pca
by photochemsyn 4y ago
If you wanted to do this with python:
cat beef.txt | grep "lasagna" | sort -n | uniq
The correct way would seem to be to write four python programs, named pcat, pgrep, psort, and puniq, and then pipe the output of one program to the other program. The only advantage would be that it should be able to work in any operating system with a python environment.
Ideally each program would have the same functionality and resilience as the original bash programs. The argparse module (for accepting the same options at the command line as the origals) and pathlib module (for portability to Windows as well as Linux and Mac) would be useful.
Now, is it really worth the trouble of doing this? Possibly, at least as part of an educational Python program. Implementing all of grep's functionality would be a big job, however.
- marcosdumay 4y agoYou can fork and pipe between python functions perfectly well. It just needs a lot of boilerplate (that you can easily abstract away). Also, you will get most of the problems of piping in Bash imported into Python. But the real question is why are you piping those? Almost always, piping is not the end-goal, and this is certainly one of those cases.