Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.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; 'preferably': 0.03; 'string.': 0.04; 'python:': 0.05; 'python?': 0.05; 'char': 0.07; 'received:verizon.net': 0.07; 'terry': 0.07; 'python': 0.08; 'builtin': 0.09; 'command-line': 0.09; 'command.': 0.09; 'eof': 0.09; 'finishes': 0.09; 'newline': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'solution,': 0.09; 'subsequent': 0.15; '"import': 0.16; '"while': 0.16; "'n'": 0.16; '*nix': 0.16; 'illegal.': 0.16; 'newlines': 0.16; 'pipe,': 0.16; 'reedy': 0.16; 'subject:command': 0.16; 'wrote:': 0.16; 'that?': 0.18; 'convert': 0.19; 'jan': 0.19; 'seems': 0.20; '(most': 0.21; 'mechanism': 0.21; 'header:In-Reply-To:1': 0.22; 'work,': 0.23; 'literal': 0.23; 'pm,': 0.24; 'command': 0.24; 'shell': 0.24; 'traceback': 0.24; 'expect': 0.25; 'says': 0.25; 'code': 0.25; 'string': 0.26; 'tried': 0.26; 'work.': 0.27; 'otherwise.': 0.28; 'print': 0.29; 'discussion.': 0.30; 'separated': 0.30; 'least': 0.31; 'changing': 0.31; 'error': 0.32; 'propose': 0.32; 'does': 0.32; 'it.': 0.33; 'there': 0.33; 'to:addr:python-list': 0.33; 'named': 0.33; 'however,': 0.34; 'header:User-Agent:1': 0.34; 'idea': 0.34; 'doc': 0.34; 'last):': 0.34; 'header:X-Complaints-To:1': 0.35; 'similar': 0.35; 'file': 0.36; 'another': 0.37; 'statements': 0.37; 'passed': 0.37; 'put': 0.37; 'something': 0.37; 'received:org': 0.38; 'some': 0.38; 'subject:: ': 0.39; 'header :Mime-Version:1': 0.39; 'missing': 0.39; 'to:addr:python.org': 0.39; 'more': 0.60; 'your': 0.61; 'illegal': 0.63; 'designed': 0.65; 'proposal': 0.67; 'subject:line': 0.73; 'subject:One': 0.77; 'programs,': 0.80; 'jon': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: One line command line filter Date: Mon, 05 Sep 2011 18:21:56 -0400 References: <4725b2c3-d930-4fb0-9fe7-a286d150f9c5@d18g2000yqm.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 In-Reply-To: <4725b2c3-d930-4fb0-9fe7-a286d150f9c5@d18g2000yqm.googlegroups.com> 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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1315261387 news.xs4all.nl 2428 [2001:888:2000:d::a6]:34662 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12794 On 9/5/2011 4:38 PM, Jon Redgrave wrote: > It seems unreasonably hard to write simple one-line unix command line > filters in python: > > eg: ls | python -c " print x.upper()" > > to get at sys.stdin or similar needs an import, which makes a > subsequent for-loop illegal. > python -c "import sys; for x in sys.stdin(): print x"<<- SyntaxError > > Am I missing something obvious? The doc says "-c Execute the Python code in command. command can be one or more statements separated by newlines," However, I have no idea how to put newlines into a command-line string. Changing '; ' to '\n' does not work, at least not in Windows. The '\n' is passed on to Python as 2 chars, and the '\' is seen as the line-continuation char and the 'n' as illegal following it. Will a *nix shell 'cook' the string and convert '\n' to a literal newline before passing it to Python? For *nix, I would expect the <", line 1, in EOFError: EOF when reading a line Perhaps tolerable if Python is at the end of the pipe, not otherwise. > Is there a better solution - if not is this worth a PEP? PEPs have to propose a concrete solution, preferably with some previous discussion. I take is that your proposal would be to add another builtin means to access stdin. -- Terry Jan Reedy