473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

using python on the command line with 'here-documents' and pipes

Hi all,

I would like to use python for a replacement for some binutils. I would
like to be able to pipe things into python. Actually I would not like
writing a 'script' to handle the input, but write the python commands
right onto the command line.

It would be possible to use here-documents (for having the correct
identation during e.g while loops) for writing python code on
the fly, but then how and where to get the pipe output:

echo -e 'line1\nline2' | python <<EOF ....

Does not really work...

Would anybody have an idea how to use python like e.g. awk etc. As a
tool within, where I can handle the stdin data, and write the code right
there (e.g. with here documents)?
thanks a lot
calmar

--
calmar
(o_ It rocks: LINUX + Command-Line-Interface
//\
V_/_ http://www.calmar.ws
Jul 18 '05 #1
6 2496
Hi,

This is from C:\Python23\Lib\site-packages\win32\Demos\pipes\cat.py:

'''cat.py
a version of unix cat, tweaked to show off runproc.py
'''

import sys
data = sys.stdin.read(1)
sys.stdout.write(data)
sys.stdout.flush()
while data:
data = sys.stdin.read(1)
sys.stdout.write(data)
sys.stdout.flush()
# Just here to have something to read from stderr.
sys.stderr.write("Blah...")

# end of cat.py

-pekka-

calmar wrote:
Hi all,

I would like to use python for a replacement for some binutils. I would
like to be able to pipe things into python. Actually I would not like
writing a 'script' to handle the input, but write the python commands
right onto the command line.

It would be possible to use here-documents (for having the correct
identation during e.g while loops) for writing python code on
the fly, but then how and where to get the pipe output:

echo -e 'line1\nline2' | python <<EOF ....

Does not really work...

Would anybody have an idea how to use python like e.g. awk etc. As a
tool within, where I can handle the stdin data, and write the code right
there (e.g. with here documents)?
thanks a lot
calmar

Jul 18 '05 #2
On Sun, 14 Nov 2004 18:12:42 +0100, calmar wrote:
It would be possible to use here-documents (for having the correct
identation during e.g while loops) for writing python code on the fly, but
then how and where to get the pipe output:

echo -e 'line1\nline2' | python <<EOF ....

Does not really work...

Would anybody have an idea how to use python like e.g. awk etc. As a tool
within, where I can handle the stdin data, and write the code right there
(e.g. with here documents)?


Can you post a working example for awk? I'll admit I'm not a shell expert,
but it seems like you are trying to stuff two files onto stdin, the "echo"
result and the python program. I don't understand how that could ever
work, and now you've piqued my curiosity.

Jul 18 '05 #3
calmar <ca****@calmar.ws> wrote:
I would like to be able to pipe things into python.
Python is perfectly happy reading stdin and writing to stdout, so it's
also perfectly happy acting as a component in a pipe.
Actually I would not like
writing a 'script' to handle the input, but write the python commands
right onto the command line.
You can do this with the -c command line option:

$ python -c 'print "hello world"'
hello world
It would be possible to use here-documents (for having the correct
identation during e.g while loops) for writing python code on
the fly, but then how and where to get the pipe output:
I think you're confusing two different things: where python gets the
code it's going to execute, and where it gets the input it's going to
feed to that executing code. You can use here-is documents for the
input, and -c for the code, and end up with something like this:

$ python -c 'import sys for line in sys.stdin:
print line[:10]

' << EOF
line one of many
line two of many
line three of many
EOF line one o
line two o
line three

Whether or not the single-quote mechanism reads past newlines and
properly preserves indentation is more a function of what shell you're
using than the python interpreter itself (the above was done with bash).
Would anybody have an idea how to use python like e.g. awk etc. As a
tool within, where I can handle the stdin data, and write the code right
there (e.g. with here documents)?
Well, the above will work, but I don't think it's a particularly good
idea. I think you would do better putting the text of your python
script in a .py file, make the first line #!/usr/bin/python, and make it
executable. Then you've got a stand-alone program that you can edit and
maintain apart from the shell script that surrounds it. In the long
run, this is likely to be simpler to test and to maintain.

Of course, if you want to be a bit perverse, you can always do:

$ python /dev/stdin << EOF print "hello, bizarre world"
EOF

hello, bizarre world

but I think that's more of a curiosity than a useful tool. Along the
same lines, I once knew somebody who linked /dev/tty.c to /dev/tty. He
could then do "cc /dev/tty.c" and type in c code directly to the
compiler". He was a sick puppy.
Jul 18 '05 #4
On 2004-11-14, Roy Smith <ro*@panix.com> wrote:

hi,
$ python -c 'import sys
for line in sys.stdin:
print line[:10]

' << EOF
line one of many
line two of many
line three of many
EOF line one o
line two o
line three


Yeah, that works nicely.

Actually, I didn't find the solution to just press enter (after a -c
'..) and write the idented code I want.
executable. Then you've got a stand-alone program that you can edit and
maintain apart from the shell script that surrounds it. In the long
run, this is likely to be simpler to test and to maintain.


Yeah, true.
Thanks a lot to all!

--
calmar
(o_ It rocks: LINUX + Command-Line-Interface
//\
V_/_ http://www.calmar.ws
Jul 18 '05 #5
On 2004-11-14, Jeremy Bowers <je**@jerf.org> wrote:
hi,
Can you post a working example for awk? I'll admit I'm not a shell
expert,
but it seems like you are trying to stuff two files onto stdin, the
"echo"
result and the python program. I don't understand how that could ever
work, and now you've piqued my curiosity.


Well, it would be something like (easy example):

echo -e 'line1\nline2\nline3' | python -c '
import sys
for line in sys.stdin:
print line'

That works now (with bash). I get the text into python and can do there
whatever I want. Awk/Sed/grep or whatever kind of tasks.

My problem, I didn't find the solution to just write the code after the
-c '...' including newlines. Sooo easy, but I didn't find that solution
(not even after hours doing curious stuff:)

Cheers
calmar

PS: would there be a smarter way to get the
stdin lines into a loop?

--
calmar
(o_ It rocks: LINUX + Command-Line-Interface
//\
V_/_ http://www.calmar.ws
Jul 18 '05 #6
calmar <ca****@calmar.ws> wrote:
echo -e 'line1\nline2\nline3' | python -c '
import sys
for line in sys.stdin:
print line'


The fileinput module is useful, it reads from stdin or from the files
provided on the command line (sys.argv), eg

$ python -c '
import fileinput, sys
for line in fileinput.input():
# Your code to manipulate line here
sys.stdout.write(line)
'
This is a line
This is another line
This is a line
This is another line

(The above is (almost) equivalent to perl -pe '# Your code here' BTW
which was in itself derived from the awk usage I think. The almost
comes in when stdin is from the terminal - you need to press CTRL-D
twice for the python version and once for the perl version)

--
Nick Craig-Wood <ni**@craig-wood.com> -- http://www.craig-wood.com/nick
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kay Lee | last post by:
Hi, I looked up os module to find out some method to move and copy files in python, but os doesn't support such methods. Is there any way to move & copy files in python? Thanks in adv.
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
15
by: Ashot | last post by:
This is sort of both Python and Vim related (which is why I've posted to both newsgroups). Python related: ---------------------- I have been frustrated for quite some time with a lack of a...
75
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the...
4
by: tnoell | last post by:
Hi comp.lang.python: New to the group and new to python, so don't tear me up too much ... I installed the GNU readline support in python2.4, and it is working, but there is one annoying behaviour...
0
by: ycollet | last post by:
Hello, I'm trying to write a program to send python statements to a python server via tcp and then get back results via a tcp connection. It nearly works ... but I'm totally lost with the...
9
by: PengYu.UT | last post by:
Hi, I feel argparse has some useful things that optparse doesn't have. But I can't find it argparse in python library reference. I'm wondering when it will be available in the python standard...
4
by: W. Watson | last post by:
I have about a 1600 line Pythron program I'd like to make some simple mods to, but have really just a nodding acquaintance with Python and Tkinter. I know quite a few languages, including C++....
0
by: Cameron Simpson | last post by:
On 17Aug2008 21:25, John Nagle <nagle@animats.comwrote: Because $HOSTNAME is a bash specific variable, set by bash but NOT EXPORTED! Like $0 and a bunch of other "private" variables, subprocesses...
3
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.