Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8050969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:12:14+00:00 2026-06-05T07:12:14+00:00

I am using a python script to make a system call to zgrep and

  • 0

I am using a python script to make a system call to zgrep and only print the first result using the -m1 option.

Script:

#! /usr/bin/env python2.7

import subprocess

print subprocess.check_output("zgrep -m1 'a' test.txt.gz", shell=True)

Error:

When running the script on large files (+2MB), the following error is generated.

> ./broken-zgrep.py

gzip: stdout: Broken pipe
Traceback (most recent call last):
  File "./broken-zgrep.py", line 25, in <module>
    print subprocess.check_output("zgrep -m1 'a' test.txt.gz", shell=True)
  File "/usr/intel/pkgs/python/2.7/lib/python2.7/subprocess.py", line 537, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command 'zgrep -m1 'a' test.txt.gz' returned non-zero exit status 2

However, if I copy the command that python complains about and run it in the shell directly, it works fine.

> zgrep -m1 'a' test.txt.gz
0000000 8c82 524d 67a4 c37d 0595 a457 b110 3192

The command’s exit status is 0 after manually running in the shell, which indicates a success. Python says the command exits with error code 2.

> echo $?
0

Here is how to make an example test file to reproduce the error. It creates a 100000 line hex file of random vaules and uses gzip to compress it.

cat /dev/urandom | hexdump | head -n 100000 | gzip > test.txt.gz

Seemingly irrelevant changes that will prevent the error:

  • Make a smaller test file

    cat /dev/urandom | hexdump | head -n 100 | gzip > test.txt.gz

  • Running without the -m1 option (warning: will spam terminal)

    print subprocess.check_output("zgrep 'a' test.txt.gz", shell=True)

  • Using grep instead of zgrep on an uncompressed file

    cat /dev/urandom | hexdump | head -n 100000 > test.txt

    print subprocess.check_output("grep -m1 'a' test.txt", shell=True)

  • Running the equivalent command in perl

    perl -e 'print `zgrep -m1 'a' test.txt.gz`'


I don’t know why the combination of python, zgrep, -m option, and large files produces this error. If any of these factors is eliminated, then there is no error.

My best guess about the cause is from reading the grep man page about the -m option.

   -m NUM, --max-count=NUM
          Stop reading a file after NUM matching lines.  If the  input  is
          standard  input  from a regular file, and NUM matching lines are
          output, grep ensures that the standard input  is  positioned  to
          just  after the last matching line before exiting, regardless of
          the presence of trailing context lines.  This enables a  calling
          process  to resume a search.  When grep stops after NUM matching
          lines, it outputs any trailing context lines.

I originally assumed that the -m option would simply cause grep to exit after finding NUM matches. But maybe there is something funny going on with grep and standard input. This still doesn’t explain why the error only occours for large compressed files though.

I ended up porting my script from python to perl to get around this problem, so there is not any immediate need for a solution. But I would really like to gain a better understanding of why this perfect storm of circumstances fails.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:12:15+00:00Added an answer on June 5, 2026 at 7:12 am

    zgrep is just a shell script, it is roughly equivalent to gunzip test.txt.gz | grep -m1 'a'. The gunzip just extracts chunks and passes them on to grep. Then, when grep finds the pattern, it exits.

    If gunzip hasn’t finished uncompressing the file by then, future writes to the stdout of gunzip (which is connected to the stdin of grep) will fail. This is precisely what’s happening in your case:

    gzip: stdout: Broken pipe
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple script using python-daemon which prints to sys.stdout : #!/usr/bin/env python
I want to make a simple batch script using python using os.system. I am
I have a script a.py : #!/usr/bin/env python def foo(arg1, arg2): return int(arg1) +
In my Python script I make an external call using subprocess.Popen(). If the call
I've got an error when I'm using passthru() to call a python script (using
I am trying to run a python script using :- python scriptname.py someoptions When
I want to search for images using a Python script and pull them off
I'm trying to schedule a number of jobs in linux using a python script.
I am trying to run a command from Python script(using Popen()) get the output
I am calling an external program within Python script using subprocess. The external program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.