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

Hey Thanks for the replies. My whole idea is that I have a folder called C:\myFolder which has some files. Now i want to scan each files' contents in this folder for a particular expression, say myExp.Process that expression and pick a word from it which is a file name. Then with this new file name (ex: a and append .txt to it), I have to search it in a list of directories that will be present in an array with me in perl. Check their presence and if they are present check for the above expression myExp2. Thus in a way there can be recursive search. For this if I scan line-by-line in a perl program then it will just slightly better than a C program. Hence i wanted to search the whole file initially with a command which will take an expression and search the whole file ,something like grep. Please help me on the same. Thanks in advance, Nishith

Original Question:

I want to do a grep like search in perl on windows platform.But I dont want to scan each line of a file for a particular expression.How can I do it. Please help. Thanks in advance. Nishith

Edit by castaway - original question restored.

Replies are listed 'Best First'.
Re: Grep-Like Activity
by holli (Abbot) on Mar 04, 2005 at 09:51 UTC
    perl -ne "print if /expression/" filename
    where "expression" is the regex you want to search for. check out perl -h for more info on command line options.

    Update:
    Oh, ...donīt want to scan each line. I misread that.
    Mmh. how else would you want to do it? Can you clarify.


    holli, /regexed monk/
      Hey Thanks for the replies. My whole idea is that I have a folder called C:\myFolder which has some files. Now i want to scan each files' contents in this folder for a particular expression, say myExp.Process that expression and pick a word from it which is a file name. Then with this new file name (ex: a and append .txt to it), I have to search it in a list of directories that will be present in an array with me in perl. Check their presence and if they are present check for the above expression myExp2. Thus in a way there can be recursive search. For this if I scan line-by-line in a perl program then it will just slightly better than a C program. Hence i wanted to search the whole file initially with a command which will take an expression and search the whole file ,something like grep. Please help me on the same. Thanks in advance, Nishith
        For this if I scan line-by-line in a perl program then it will just slightly better than a C program.
        Youīre wrong. To scan the whole file you will have to slurp it into memory first. Thatīs bad, especially when the target for myExp is likely to be near the beginning of the file.
        However, here is how to read a file at once and apply a regex to the content:
        use strict; my $data; slurpfile ("yourfile.txt", $data); print $1 if $data =~ /(expression)/m; sub slurpfile { local $/ = undef; open IN, "<", $_[0] or return 0; $_[1] = <IN>; close IN; return 1; }


        holli, /regexed monk/
Re: Grep-Like Activity
by ambrus (Abbot) on Mar 04, 2005 at 09:56 UTC

    You can use a2p to find it out. This simple awk script prints lines matching a regexp:

    /foo/
    It you feed this to a2p, you get some magic header and this perl script:
    while (<>) { print $_ if /foo/; }
Re: Grep-Like Activity
by manav (Scribe) on Mar 04, 2005 at 09:53 UTC
    Then what do you need to scan on?? How do you create a subset of a file on which you grep....by the way, in Perl, the grep doesnt work on files, only filehandles. If your file is small, you are probably better off filtering out your subset of lines from the file, storing them into an array and grepping on it.

    Manav