473,602 Members | 2,876 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

execute shell command pipeline

Hi,

I'm a Python newbie and I'm wondering how I can do the following Perl thing:

open(F, 'grep -b 2004 logfile | head -1 | cut -d: -f1 |') or die "blah, blah";
$offset = <F>;
( or $offset = `grep -b 2004 logfile | head -1 | cut -d: -f1`; )

in Python. When I tried:

offset = os.popen('grep -b 2004 logfile | head -1 |cut -d: -f1').read()

I get a whole bunch of error messages --

grep: writing output: Broken pipe

Any help would be most appreciated.

Regards,

Paul
Jul 18 '05 #1
3 3883
In article <87************ **************@ posting.google. com>,
Paul <Pa******@telus .com> wrote:
Hi,

I'm a Python newbie and I'm wondering how I can do the following Perl thing:

open(F, 'grep -b 2004 logfile | head -1 | cut -d: -f1 |') or die "blah, blah";
$offset = <F>;
( or $offset = `grep -b 2004 logfile | head -1 | cut -d: -f1`; )

in Python. When I tried:

offset = os.popen('grep -b 2004 logfile | head -1 |cut -d: -f1').read()

I get a whole bunch of error messages --

grep: writing output: Broken pipe

Jul 18 '05 #2
The Python library manual says (http://python.org/doc/lib/module-signal.html)
Python installs a small number of signal handlers by default: SIGPIPE
is ignored (so write errors on pipes and sockets can be reported as
ordinary Python exceptions) and SIGINT is translated into a
KeyboardInterru pt exception. All of these can be overridden.
In perl, or from the shell, grep is terminated by SIGPIPE. In Python,
grep ignores the signal because the "ignored" status of SIGPIPE is
inherited by the child process. Instead, it prints the error once for
each write that returns -EPIPE.

Jeff

Jul 18 '05 #3
Paul wrote:
Hi,

I'm a Python newbie and I'm wondering how I can do the following Perl thing:

open(F, 'grep -b 2004 logfile | head -1 | cut -d: -f1 |') or die "blah, blah";
$offset = <F>;
( or $offset = `grep -b 2004 logfile | head -1 | cut -d: -f1`; )

in Python. When I tried:

You don't have to do it this verbosely, but it works:

import os
text = '2004'
filename = 'logfile'
delim = ':'
matches = os.popen('grep -b %s %s' % (text, filename)).read lines()
if matches:
first = matches[0]
offset = first.split(del im)[0]
# note: offset is now a string

// m

Jul 18 '05 #4

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

Similar topics

1
11002
by: Valentine Kouznetsov | last post by:
Hi, simple question, how to execute command in current shell, not in subshell? Example. I have environment variable A=aaa and need to invoke shell (sh) script which will do something with A. If I do os.system() then sub-shell will setup A and execute my script there leaving A in parent shell untouched. Thanks,
2
3364
by: eichin | last post by:
One of my recent projects has involved taking an accretion of sh and perl scripts and "doing them right" - making them modular, improving the error reporting, making it easier to add even more features to them. "Of course," I'm redoing them in python - much of the cut&paste reuse has become common functions, which then get made more robust and have a common style and are callable from other (python) tools directly, instead of having to...
8
2743
by: Neale | last post by:
In a python program, I want to do a Unix directory list and then append selected files to each other which, as you know, is just "cat filename2 >> filename1." How do I escape into the shell and then return, bringing with me a directory list? Where can I find some examples of mixing shell commands and Python?
4
9888
by: Kartik | last post by:
Hi, I have an ASP.NET application using VB.NET.I am sending a DOS command to a machine on the network to print a file. This is achieved using xp_cmdshell Dim str As String = "xp_cmdshell ""type " & nwkfilepath & " > " & pPrinterName & " """
2
1750
by: bearophileHUGS | last post by:
This can be of little interest of some people here, but I think it can be interesting enough to justify a post. An article on the Microsoft Command Shell: http://arstechnica.com/guides/other/msh.ars (Its syntax is rather Python-like, but Py syntax seems better to me, even taking into account that their purposes are a little different). Other info: http://en.wikipedia.org/wiki/MSH_(shell)
9
78273
by: sohan | last post by:
Hi, I want to know how to connect and execute a db2 query from inside a UNIX shell script. Details: We have a unix shell script. We need to execute multiple db2 sql queries from this shell script and export the result to a file. Any code snippet on this will be helpful.
3
3809
by: George Sakkis | last post by:
I'm trying to figure out why Popen captures the stderr of a specific command when it runs through the shell but not without it. IOW: cmd = if 1: # this captures both stdout and stderr as expected pipe = Popen(' '.join(cmd), shell=True, stderr=PIPE, stdout=PIPE) else: # this captures only stdout pipe = Popen(cmd, shell=False, stderr=PIPE, stdout=PIPE) # this prints the empty string if not run through the shell
1
3549
by: raocheng | last post by:
Please see the following code. Suppose I have many shell commands to be executed. And I don't want to fork a sub shell for each command(eg: status,output = commands.getstatusoutput(cmd)) because it is too expensive. I want to use only one sub shell to execute all these commands and want to get each command's output. How can I accomplish this task ? Thanks in advance. =========================================== #!/usr/bin/env python
4
15738
by: coetzer | last post by:
Help please I am trying to execute a shell.exe which requires a command for usage in VB.Net so the shell is in folder "tmp" c:\tmp> shell.exe and then the command follows: c:\tmp> shell.exe -f "c:\file\to\load" table database There are of exsamples for just executing a shell but can't seem to come right with shell followed by command.
0
7921
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8056
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6730
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5870
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3901
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1511
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.