I am interested in using Python as a stream editor, similar to the way that perl can do so with the -ne flag, or that Sed and Awk can do. I tried a number of modules out there for doing Python one liners, such as:
Oneliner: https://github.com/gvalkov/python-oneliner
pyp: https://code.google.com/p/pyp/
pyle: https://pypi.python.org/pypi/pyle/0.1
I also modified this small module (I named it ne.py) from here: https://stackoverflow.com/a/17197610/3923510 , by https://stackoverflow.com/users/168740/matt-mcclure to emulate perl -ne.
For example:
cat input_file.txt|python -mne 'line=line.rstrip();values = line.split("\t");print (values[1])'
is equivalent to
cat input_file.txt|perl -ne 'chomp $_; @a=split /\t/, $_;print "$a[1]\n"'
and both will successfully print the second element of each tab delimited line.
However, it seems that none of these modules allow any control statements (if, while, for functions) within them.
For example, I modified the previous Python code to only print the second element if it is greater than 1.
cat input_file.txt|python -mne 'line=line.rstrip();values = line.split("\t");if (values[1]>1): print (values[1])'
This produced a syntax error, while the perl version
cat input_file.txt|perl -ne 'chomp $_; @a=split /\t/, $_;print "$a[1]\n" if ($a[1]>1)'
worked fine. Oneliner, pyp and pyle all had this problem as well. I think that the Python error happens because Python has significant whitespace while perl does not.
Is there a better module out there for using Python from the terminal, or can I easily fix the module I am using? If not, is is possible that Python is simply not as powerful as perl/Sed/Awk in this regard?
People have already discussed this problem here:
Python equivalent to perl -pe?
and the duplicate question I asked:
Python equivalent to perl -ne switch
but I haven't seen any discussion of the modules out there to deal with this problem.
The module ne.py is:
from __future__ import print_function
import re
import sys
import math
for line in sys.stdin:
exec(sys.argv[1], globals(), locals())
try:
line,
except:
sys.exit('-p destination: $!\n')