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

GREP Question

by jjdeterick (Curate)
on Apr 23, 2001 at 16:07 UTC ( #74693=perlquestion: print w/ replies, xml ) Need Help??
jjdeterick has asked for the wisdom of the Perl Monks concerning the following question:

I have a basic question about the GREP function. I'm trying to grep a file for the pattern "[1]", but GREP is interpreting the [ and ] characters. I tried 'grep /\[1/\] filename' and that does not work either. Any help is greatly appreciated!

Edit 2001-04-23 by tye

Edit: chipmunk 2001-04-24

Comment on GREP Question
Download Code
Re: GREP Question
by c-era (Curate) on Apr 23, 2001 at 17:01 UTC
    This isn't a perl question, but this will work: grep /\[1\]/ filename
      It will almost work, but you need a comma...
      grep /\[1\]/, filename;
      Update voted down? Why? ok, it should probably be @filename, but I just copied it, you do need the comma.
                      - Ant

        Probably the downvote because, as indicated (albeit somewhat indirectly), the response concerns unix grep(1), not perl grep, so the comma is not needed. Were it to be in perl, and had the file been read into an array using @file = <>, then grep /\[l\]/, @file would be correct, but from the command line, it ain't.

        However, the original response is also wrong: since we're on the command line, the slashes are read as part of the regular expression, not as a pattern delimiter, so what you actually want is either

        #!/usr/bin/perl open FH, "filename" or die "couldn't open file.\n"; @filename = <>; print grep /\[l\]/,@filename;
        or (note single quotes)
        % grep '\[l\]' filename
        You will note which of them is simpler.

        If God had meant us to fly, he would *never* have give us the railroads.
            --Michael Flanders

(tye)Re: GREP Question
by tye (Cardinal) on Apr 23, 2001 at 20:42 UTC

    Just a quick note that, despite what many have appeared to assume, this may not be a question about the "grep" program as the noder refers to it as "the GREP function". It may simply be that the noder doesn't understand that Perl's grep works on a list of scalars (not on filenames) and requires either braces or a comma (or that was left out when the example was retyped).

    jjdeterick should definitely take more care in crafting future questions (cutting and pasting the exact code involved and exactly what output and/or error is generated).

            - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-04-20 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls