473,584 Members | 2,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2510
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.writ e(data)
sys.stdout.flus h()
while data:
data = sys.stdin.read( 1)
sys.stdout.writ e(data)
sys.stdout.flus h()
# Just here to have something to read from stderr.
sys.stderr.writ e("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.co m> 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.or g> 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\n line3' | 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\n line3' | 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.writ e(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
86464
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
8072
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? Thank you, -- greetz tom
15
7374
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 history command in IDLE (in fact with IDLE in general). Often I'll develop new code at the command line, testing each line as I go. Currently I have...
75
4622
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 functions are simplified versions of the full featured methods for compiled regular expressions. Most non-trivial applications always use the compiled...
4
2902
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 that I am hoping to squash ... Namely, when I hit <esc> to go to edit mode, then hit 'k' to go up in the command history, the prompt is put at...
0
1496
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 embedded dictionary (I'm quite new to python). The first part of the server start the python interpreter via Py_Initialize() and then waits for python...
9
3008
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 installation. Thanks, Peng
4
1296
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++. Let's change that. I've not used anything but C in recent years, and C++ was in my bag before that along with a number of aged languages. The biggest...
0
3463
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 do not inherit this value. From "man bash": Shell Variables The following variables are set by the shell:
3
3907
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 this I mean that the format would be something like this: You have Page1 : This has 2-3 buttons on it. Clicking on each button opens up a new...
0
7897
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7829
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8189
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8198
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6587
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5705
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5379
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1438
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.