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

Strange grep and matching behaviour...

by Clownburner (Monk)
on May 22, 2002 at 21:12 UTC ( [id://168584]=perlquestion: print w/replies, xml ) Need Help??

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

Ok, I've beat my head against this for an hour. I may be on crack, and just missing something simple. Please let me know if that's the case. This little number is supposed to read a file and return only the lines that do not contain the string 'fubar'.
sysopen (FILE,$filename,O_RDONLY) or die "Error! $!\n"; my (@slurp) = <FILE>; close FILE; foreach my $foo (@slurp) { unless ($foo =~ /fubar/i) {print $foo; } }
It returns all the lines, even the ones with 'fubar' in them. I've tried using /ig instead of just /i, no change. I have also tried this:
sysopen (FILE,$filename,O_RDONLY) or die "Error! $!\n"; my (@slurp) = <FILE>; close FILE; my (@lines) = grep !/fubar/, @slurp; foreach my $foo (@lines) { print $foo; }
That didn't work either. When I reversed the grep (changed it to print only lines that matched), nothing printed at all. So it's the grep / match that's failing. The 'fubar' string is in the text, and it's not specially hidden - in fact it's padded with white space. So, what the heck is wrong here? What am I doing wrong? If it's relevant, I'm running perl, version 5.005_03. TIA
"Non sequitur. Your facts are un-coordinated." - Nomad

Replies are listed 'Best First'.
Re: Strange grep and matching behaviour...
by Ovid (Cardinal) on May 22, 2002 at 21:24 UTC
    my @slurp = grep { ! /fubar/ } <FILE>;

    And here's what I used to test it:

    my @slurp = grep { ! /fubar/ } <DATA>; use Data::Dumper; print Dumper \@slurp; __DATA__ asdffubarasdf 1 as;ldja;slk;alskj 2 as;dlkfjfubarasdf 3 as;ldfkj;laksf;lkasj 4 asfd;aslfk 5 fubar 6

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Strange grep and matching behaviour...
by virtualsue (Vicar) on May 22, 2002 at 21:28 UTC
    Try something like :
    #!/usr/bin/perl use warnings; use strict; open FH, "testit" or die $!; my @lines = <FH>; for (@lines) { print $_ if (!/fubar/) ; }
    Though I don't see why your grep example didn't work. (my @nonfubar = grep !/fubar/, @lines;)
Re: Strange grep and matching behaviour...
by buckaduck (Chaplain) on May 22, 2002 at 21:26 UTC
    This may or may not be your issue, but it has certainly happened to me many times... But maybe Perl is right and you are wrong. Perhaps the file does not really contain lines with "fubar". Even if it looks like "fubar" is there, are you sure that there aren't control characters hidden in the file? e.g. ("fu^Abar")? It might be worthwhile to check.

    And since I'm assuming that you aren't really looking for the literal text "fubar", there are other possible issues. Are you matching whitespace characters correctly? Is this a multi-line pattern? Maybe you should give us an exact example.

    Update: 200th post!

    buckaduck

      I'll second buckaduck's speculations -- I tried Clownburner's code myself, copied directly from his post but using a file name and search pattern of my own, and the result was exactly as expected; i.e. the same as running unix grep with the given pattern on the given file.
Re: Strange grep and matching behaviour...
by vladb (Vicar) on May 22, 2002 at 21:27 UTC
    How about this:
    sysopen (FILE,$filename,O_RDONLY) or die "Error! $!\n"; my (@slurp) = grep !/fubar/,<FILE>; close FILE;
    Let the almighty grep take care of filtering all lines containing 'foobar' out. The match should do it just fine.

    I tested similar script with this command:
    echo 'fubar\nfoo' | perl -e '@stuff=grep \!/fubar/, <STDIN>;print@stuf +f;'
    This would only print:
    foo
    and filter the 'foobar' line out! ;-)

    UPDATE: owww, and how did I miss so many earlier posts!? I'm sorry Ovid, I didn't notice your post which makes my look like nothing but straight copy. ;/.

    _____________________
    $"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/;$_=&#91"ps -e -o pid | "," $2 | "," -v "," "&#93;`@$_`?{print" ++ $1"}:{print"- $1"}&&`rm $1`;print"\n";}

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-18 09:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found