473,382 Members | 1,396 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,382 software developers and data experts.

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 3872
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
KeyboardInterrupt 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)).readlines()
if matches:
first = matches[0]
offset = first.split(delim)[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
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...
2
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...
8
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...
4
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...
2
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:...
9
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...
3
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...
1
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...
4
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.