converting a sed / grep / awk / . . . bash pipe line into python
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Tue Sep 2 14:26:38 EDT 2008
On Tue, 02 Sep 2008 10:36:50 -0700, hofer wrote:
> sed 's/\.\..*//' \ ### remove '//' comments | sed 's/#.*//'
Comment does not match the code. Or vice versa. :-)
Untested:
from __future__ import with_statement
from itertools import ifilter, ifilterfalse, imap
def is_junk(line):
line = line.rstrip()
return not line or line.startswith('//') or line.startswith('#')
def extract_numbers(line):
result = map(int, line.split()[:2])
assert len(result) == 2
return result
def main():
with open('test.txt') as lines:
clean_lines = ifilterfalse(is_junk, lines)
pairs = imap(extract_numbers, clean_lines)
print '\n'.join(b for a, b in pairs if a + b == 42)
if __name__ == '__main__':
main()
Ciao,
Marc 'BlackJack' Rintsch
More information about the Python-list
mailing list