http://www.perlmonks.org?node_id=415562

jpk236 has asked for the wisdom of the Perl Monks concerning the following question:

I have a script which parses large files -- 800+ lines full of content. Here is the line I use to grep the pattern out:

$output = `/usr/bin/grep -B NUM PATTERN FILE | /usr/bin/head -n 1`;

where NUM is an incrementing #, PATTERN is the matching string, and FILE is the file being read. This line is in a do..until.

For some reason, randomly, grep: write error: Broken pipe or grep: writing output: Broken pipe will output to the error log. The line is still parsed correctly -- but these error logs are annoying and I'd really like to know whats causing them. Thanks in advance!

- Justin

Replies are listed 'Best First'.
Re: Grep Issues
by Zaxo (Archbishop) on Dec 17, 2004 at 06:37 UTC

    The broken pipe happens when head reads its one line and quits. That generates SIGPIPE to the system grep process. It's harmless, you can redirect local(*STDERR) to /dev/null in a suitable scope if you don't want to see the warning

    Why not do that in perl? It's easy.

    my ($output, @lines) = ''; my ($num, $re, $file) = @whatever; open my $fh, '<', $file or die $!; while (<$fh>) { push @lines, $_; shift @lines if @lines > $num; # $output = join('', @lines), last if /$re/; $output = $lines[0], last if /$re/; } close $fh or die $!;
    Untested.

    Update: I realized that my code didn't do exactly what OP's does. Original line commented out and correction added.

    After Compline,
    Zaxo

Re: Grep Issues
by eyepopslikeamosquito (Archbishop) on Dec 17, 2004 at 06:29 UTC

    I'd say the head command exits as soon as it's written its one line. After that, grep is writing to a pipe with no reader (because head has exited) -- hence the "broken pipe".

    Why not write the whole thing in Perl? You don't need no damn grep and head if you've got perl. :-)