perl - Modules of Python Stream Editing -


i interested in using python stream editor, similar way perl can -ne flag, or sed , awk can do. tried number of modules out there doing python 1 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 modified small module (i named ne.py) here: https://stackoverflow.com/a/17197610/3923510 , https://stackoverflow.com/users/168740/matt-mcclure emulate perl -ne.

for example:

cat input_file.txt|python -mne  'line=line.rstrip();values = line.split("\t");print (values[1])' 

is equivalent

cat input_file.txt|perl -ne 'chomp $_; @a=split /\t/, $_;print "$a[1]\n"' 

and both print second element of each tab delimited line.

however, seems none of these modules allow control statements (if, while, functions) within them.

for example, modified previous python code print second element if greater 1.

cat input_file.txt|python -mne 'line=line.rstrip();values = line.split("\t");if (values[1]>1): print (values[1])'  

this produced syntax error, while perl version

cat input_file.txt|perl -ne 'chomp $_; @a=split /\t/, $_;print "$a[1]\n" if ($a[1]>1)' 

worked fine. oneliner, pyp , pyle had problem well. think python error happens because python has significant whitespace while perl not.

is there better module out there using python terminal, or can fix module using? if not, is possible python not powerful perl/sed/awk in regard?

people have discussed problem here:

python equivalent perl -pe?

and duplicate question asked:

python equivalent perl -ne switch

but haven't seen discussion of modules out there deal problem.

the module ne.py is:

from __future__ import print_function import re import sys import math line in sys.stdin:     exec(sys.argv[1], globals(), locals())     try:         line,     except:         sys.exit('-p destination: $!\n')  

i think figured out, need add $ before command:

cat input_file.txt|python -mne $'line=line.rstrip();values = line.split("\t");\nif (values[1]>1): print (values[1])'  

hat tip https://stackoverflow.com/a/23301247/3923510


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -