execute shell command pipeline

Mark McEahern marklists at mceahern.com
Thu Mar 25 15:46:42 EST 2004


Paul wrote:

>Hi,
>
>I'm a Python newbie and I'm wondering how I can do the following Perl thing:
>
>open(F, 'grep -b 2004 logfile | head -1 | cut -d: -f1 |') or die "blah, blah";
>$offset = <F>;
>( or $offset = `grep -b 2004 logfile | head -1 | cut -d: -f1`; )
>
>in Python.  When I tried:
>
You don't have to do it this verbosely, but it works:

import os
text = '2004'
filename = 'logfile'
delim = ':'
matches = os.popen('grep -b %s %s' % (text, filename)).readlines()
if matches:
    first = matches[0]
    offset = first.split(delim)[0]
    # note: offset is now a string

// m




More information about the Python-list mailing list