?

Log in

gwen
gwenix
. ..: .: ..::.: ..:.:.....: ...::: ...::.


April 2011
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

gwen [userpic]
A lesson from today...

Perl's "grep" sucks balls.

Thank you, that is all.

Current Mood: quixoticquixotic
Current Music: Mary Ellen Makes the Moment Co-Aha
Comments
grep tease

Oh cmon, explain why?
(not that I use it much, I use regex matching most of the time)

Re: grep tease

Perl's grep behaves like grep(1) in the sense that it returns the subset of the list you pass it that passes your criteria.

It's a handy shorthand for doing foreach @array {push @new_array if (condition($_))}

Re: grep tease

Nice in theory, it wasn't actually working.

Re: grep tease

I beg to differ. Using grep, sort, and map to process lists goes a long way toward compacting code in a readable way.

Re: grep tease

grep uses regex, though when I was giving it clear regex to find certain lines out of a set, it just plain wasn't finding them.

Re: grep tease

Would you mind posting a code sample?

Re: grep tease

open (F, "somefile");
@lines = grep { /BLAH/ }, ;
close (F);

I tried a number of different iterations on that second line too, none worked. I know that "BLAH" was correct, because I could grep on that same line from outside the perlscript.

Re: grep tease

er, that's grep { /BLAH/ }, [f] (-- pretend that [] are the appropriate brackets!

I don't have time to go looking up the codes to make the right brackets now.

Re: grep tease


open (F, "somefile");
@lines = grep { /BLAH/ }, <F>;
close (F);


I am left suspecting that your regexp is wrong. I just ran that code on a file I have here, and it did exactly what you say it should do. Note that Perl regexp is different from grep(1), egrep(1), sed(1), or vi(1) regexps, so your regexp was probably right for grep(1) but not Perl.

Re: grep tease

My "regex" was the line, no funky characters, just a string literal. As I understand it, /BLAH/ is supposed to match BLAH on the line, no matter where it occurs, as it does when I'm doing if (/BLAH/) (which I have done in the past). If that's the case, then yes, it was supposed to work. It did not.

Re: grep tease


open (F, $ARGV[0]);

my @months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime time;

my $search_string = "$mday\\/$months[$mon]";

print "$search_string\n";

@lines = grep /$search_string/, <F>;
print join("", @lines);
close (F);

open (F, $ARGV[0]);
@lines = grep /18\/Jan/, <F>;
print join("", @lines);
close (F);


Does that work for you?

Duh?

That's what /bin/grep is for.

Re: Duh?

That's exactly what I did eventually... I just reverted back to using a bash script with grep and awk.

Re: Duh?

It's worth pointing out that I don't buy into the "perl is replacement for sed and awk and grep on the command line" argument. IMO, if you're writing a larger script and doing zany things with sed/awk/grep Perl would be better, but for quick command line use sed/awk/grep is much faster.