This page contains common examples of common perl-like operations (taken from Perl One-Liners Explained) not covered in Sed-Like Examples.
Print the sum of all the fields on a line.
>>> stream_vals('foo.csv', delimit = '\t') | filt(lambda tup: sum(tup)) | to_stream(sys.stdout)
Print the sum of all the fields on all lines.
>>> print stream_vals('foo.csv', delimit = '\t') | filt(lambda tup: sum(tup)) | sum_()
Find the minimum element on a line.
>>> stream_vals('foo.csv', delimit = '\t') | filt(lambda tup: min(tup)) | to_stream(sys.stdout)
Find the minimum element over all the lines.
>>> print stream_vals('foo.csv', delimit = '\t') | filt(lambda tup: min(tup)) | min_()
Find the maximum element on a line.
>>> stream_vals('foo.csv', delimit = '\t') | filt(lambda tup: max(tup)) | to_stream(sys.stdout)
Find the maximum element over all the lines.
>>> print stream_vals('foo.csv', delimit = '\t') | filt(lambda tup: max(tup)) | max_()
Replace each field with its absolute value.
>>> stream_vals('foo.csv', delimit = '\t') | filt(lambda tup: tuple(abs(e) for e in tup)) | to_stream(sys.stdout)
Find the total number of fields (words) on each line.
>>> stream_vals('foo.csv', delimit = '\t') | filt(lambda tup: len(tup)) | to_stream(sys.stdout)
Print the total number of fields (words) on each line followed by the line.
>>> stream_lines('foo.csv') | \
(csv_split(delimit = '\t') | filt(lambda l: len(l))) + relay() | \
filt(lambda (len, l): str(len) + '\t' + l) | to_stream(sys.stdout)
Find the total number of fields (words) on all lines.
>>> print stream_vals('foo.csv', delimit = '\t') | filt(lambda tup: len(tup)) | sum_()
Print the total number of fields that match a pattern.
>>> pattern = re.compile(r'foo(.*?)bar')
>>> print stream_vals('foo.csv', delimit = '\t') | \
filt(pre = lambda tup: sum((1 if pattern.match(e) else 0 for e in tup) > 0)) | sum_()
Print the total number of lines that match a pattern.
>>> print stream_vals('foo.csv', delimit = '\t') | grep(re.compile(r'foo(.*?)bar') | sum_()