Re: converting a sed / grep / awk / . . . bash pipe line intopython

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc 'BlackJack' Rintsch

    Re: converting a sed / grep / awk / . . . bash pipe line intopython

    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_nu mbers, clean_lines)
    print '\n'.join(b for a, b in pairs if a + b == 42)


    if __name__ == '__main__':
    main()

    Ciao,
    Marc 'BlackJack' Rintsch
Working...