One line command line filter

Thomas Jollans t at
Mon Sep 5 16:47:23 EDT 2011

On 05/09/11 22:38, 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?

ls | python -c "for line in __import__('sys').stdin: print (line.upper())"

More information about the Python-list mailing list