Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'output': 0.04; '"__main__":': 0.07; '__name__': 0.07; 'bash': 0.07; 'python': 0.09; 'grep': 0.09; 'modules.': 0.09; 'porting': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'subject:python': 0.11; 'itertools': 0.16; 'left,': 0.16; 'message-id:@dough.gmane.org': 0.16; 'pairs': 0.16; 'pipe:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subprocess': 0.16; 'wrote:': 0.17; 'yield': 0.17; 'shell': 0.18; 'skip:p 30': 0.20; 'translate': 0.20; 'trying': 0.21; 'import': 0.21; 'example': 0.23; 'script': 0.24; 'tried': 0.25; 'header :User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.28; 'skip:( 20': 0.28; 'kumar': 0.29; 'implement': 0.32; 'url:python': 0.32; 'print': 0.32; 'to:addr:python-list': 0.33; 'entry': 0.33; 'received:org': 0.36; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'subject:: ': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'more': 0.63; 'cut': 0.71; 'group)': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Linux shell to python Date: Mon, 30 Jul 2012 13:58:56 +0200 Organization: None References: <1343631941.7199.YahooMailNeo@web193104.mail.sg3.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084a1f8.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1343649543 news.xs4all.nl 6867 [2001:888:2000:d::a6]:45886 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26247 Vikas Kumar Choudhary wrote: > let me know if someone has tried to implement (grep and PIPE) shell > commands in python `lspci | grep Q | grep "$isp_str1" | grep "$isp_str2" > | cut -c1-7' > > I tried to use python subprocess and OS.Popen modules. subprocess is the way to go. > I was trying porting from bash shell to python. Here's an example showing how to translate a shell pipe: http://docs.python.org/library/subprocess.html#replacing-shell-pipeline But even if you can port the shell script literally I recommend a more structured approach: import subprocess import itertools def parse_data(lines): for not_empty, group in itertools.groupby(lines, key=bool): if not_empty: triples = (line.partition(":") for line in group) pairs = ((left, right.strip()) for left, sep, right in triples) yield dict(pairs) if __name__ == "__main__": def get(field): return entry.get(field, "").lower() output = subprocess.Popen(["lspci", "-vmm"], stdout=subprocess.PIPE).communicate()[0] for entry in parse_data(output.splitlines()): if "nvidia" in get("Vendor") and "usb" in get("Device"): print entry["Slot"] print entry["Device"] print