Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

Comment on Grep Issues
Select or Download Code
Re: Grep Issues
by eyepopslikeamosquito (Canon) 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. :-)

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-07-12 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (239 votes), past polls