using python with -c (as a inline execution in shell)

L

les ander

Hi,
in perl one can do all sorts of stuff in a single line form the command shell.
python has the -c option.
it is easy to do stuff like,
python -c "print 10+20"
etc statements
but suppose I want to read from the stdin (piped) i don't know how to do this since
python -c "from sys import stdin; for x in stdin: print x"

gives a syntax error.

Anyone one know how to do this?
Also, is there is site that talkes about doing this kind of stuff?
the man pages in xterm are pretty sparse and don't say that much
thanks
 
S

Skip Montanaro

les> python -c "from sys import stdin; for x in stdin: print x"

les> gives a syntax error.

les> Anyone one know how to do this?

How about:

% python -c 'from sys import stdin
> for x in stdin:
> print x
> '
hi there
bye
hi there

bye

Skip
 
A

Alex Martelli

les ander said:
but suppose I want to read from the stdin (piped) i don't know how to do
this since python -c "from sys import stdin; for x in stdin: print x"

gives a syntax error.

Anyone one know how to do this?

Not _good_ ways, but:

import sys; print sys.stdin.read()

import sys; sys.stdout.write(sys.stdin.read())

import sys; sys.stdout.writelines(sys.stdin)

are some approaches. None matches the double-spacing effect you appear
to be after, but changing every '\n' into two ain't _that_ hard, e.g.

import sys; print sys.stdin.read().replace('\n','\n\n')

etc, will give kinda the same doublespacing your approach would give if
it worked. Still, Python just isn't oneline-oriented...


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top