Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Grep Issues

by jpk236 (Monk)
on Dec 17, 2004 at 06:20 UTC ( #415562=perlquestion: print w/replies, xml ) Need Help??

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 $!;

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

    After Compline,

Re: Grep Issues
by eyepopslikeamosquito (Bishop) 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. :-)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://415562]
Approved by superfrink
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2022-12-08 20:42 GMT
Find Nodes?
    Voting Booth?

    No recent polls found