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

I have a Unix script right now that I'm converting to a Perl script (going to Microsoft). The unix script looks at a text file using grep -v and grep and writes to two seperate files depending on the outcome. The contents of the file that is getting grepped could look something like this:

order_item : CKFH6-05 : INVALID KEY ID : CKFH6-05
order_item : CKFH6-05 : *** OK *** : CKFH6-05

The grep from the unix script looked like this:
foreach i ('ls -d APPL_RMKS_RCV* ') grep -v 'NOT|INVALID|MUST|ERROR' $i | grep - >> ${good_ones} grep 'NOT|INVALID|MUST|ERROR' $i | grep - >> ${bad_ones} end
To this point, for the Perl script I have:
foreach $file(@file){ open(MYFILE, $file[$i]) or die qq(Cannot open $file[$i]\n); my $line = <MYFILE>; while ($line ne "") { #******* do grep here *******# } } $i++; }
What would be the equivalent in Perl for the grep in my unix script? Namely the grep -v. Thank you.

Replies are listed 'Best First'.
Re: Unix grep to Perl grep
by Corion (Patriarch) on Sep 12, 2003 at 13:28 UTC

    You can do two things here: Either approach the problem the same way as the shell script did, and search the file twice, once for matching lines, and once for nonmatching lines, or you can search through the file once, and put the matching lines into one list, and the nonmatching lines into another list.

    Your naming of variables is quite confusing - you shouldn't give the iterator of a loop a name and then not use it at all. I've given the iterator a different name than the list it walks through, this should make it clearer to you what is happening:

    # untested snippet my @good_lines,@bad_lines; foreach my $file_name (@file){ open(MYFILE, $file_name) or die qq(Cannot open '$file_name' : $!\n); # Read the whole file into memory my @lines = <MYFILE>; # And find the good and bad lines push @good_lines, grep { ! /NOT|INVALID|MUST|ERROR/ }, @lines; push @bad_lines, grep { /NOT|INVALID|MUST|ERROR/ }, @lines; }; print "good: @good_lines"; print "bad : @bad_lines";

    This way gives easy results, but it consumes quite a lot of memory, as every file is loaded twice into memory and we do twice as much work when searching through the lines, as every line that does not belong to the good lines must belong to the bad lines. We can change the script to follow this logic by doing it like this:

    # untested snippet my @good_lines,@bad_lines; foreach my $file_name (@file){ open(MYFILE, $file_name) or die qq(Cannot open '$file_name' : $!\n); # Read file line by line my $line; while (defined $line = <MYFILE>) { # Does the current line match our RE? if ($line =~ /NOT|INVALID|MUST|ERROR/) { push @bad_lines, $line; # store it in @bad_lines } else { push @good_lines, $line; # store it in @good_lines }; }; }; print "good: @good_lines"; print "bad : @bad_lines";

    Both methods I've shown you do not create the temp files ${good_ones} and ${bad_ones} - if you want/need this, you should print the lines directly into the respective file handles.

    As an aside, you die whenever a file can't be opened for reading, but you don't tell the user why it couldn't be opened. You should use $! as the error reason in the string.

    open(MYFILE, $file[$i]) or die qq(Cannot open $file[$i] : $!\n);
    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
      Thanks a million Corion. Works like a charm!
Re: Unix grep to Perl grep
by Limbic~Region (Chancellor) on Sep 12, 2003 at 13:24 UTC
Re: Unix grep to Perl grep
by Abigail-II (Bishop) on Sep 12, 2003 at 14:00 UTC
    There are several Unix kits for Microsoft, with working greps. It's probably easier to install such a kit than to rewrite programs.

    Abigail