Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12794

Re: One line command line filter

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 <python-python-list@m.gmane.org>
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 <tjreedy@udel.edu>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.782.1315261387.27778.python-list@python.org> (permalink)
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

Show key headers only | View raw


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 "<something>   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 <command>
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 <<EOF mechanism to work. Have you tried that?

That said, Python is designed for named multiple-statement programs,
just as it is designed for named multiple-statement functions. Or it is 
designed for interactive work.

The following works:
dir | python -c "while True: print(input())"

except that it finishes with an error traceback:
Traceback (most recent call last):
   File "<string>", line 1, in <module>
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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

One line command line filter Jon Redgrave <jredgrave@capisco.com> - 2011-09-05 13:38 -0700
  Re: One line command line filter Thomas Jollans <t@jollybox.de> - 2011-09-05 22:47 +0200
    Re: One line command line filter Jon Redgrave <jredgrave@capisco.com> - 2011-09-05 14:32 -0700
      Re: One line command line filter Terry Reedy <tjreedy@udel.edu> - 2011-09-05 19:02 -0400
  Re: One line command line filter Terry Reedy <tjreedy@udel.edu> - 2011-09-05 18:21 -0400
    Re: One line command line filter Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-06 09:18 +1000
      Re: One line command line filter Terry Reedy <tjreedy@udel.edu> - 2011-09-05 21:25 -0400
      Re: One line command line filter Hans Mulder <hansmu@xs4all.nl> - 2011-09-06 13:15 +0200
  Re: One line command line filter Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-06 09:29 +1000
  Re: One line command line filter John Wiegley <jwiegley@gmail.com> - 2011-09-06 01:51 -0500
  Re: One line command line filter Peter Otten <__peter__@web.de> - 2011-09-06 09:53 +0200

csiph-web