in reply to Grep-Like Activity

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/

Replies are listed 'Best First'.
Re^2: Grep-Like Activity
by nishith_dave (Initiate) on Mar 04, 2005 at 11:52 UTC
    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/