(top-posting fixed)
M.V.Johnson wrote:
> ----- Original Message -----
> From: "Bob Showalter" <[EMAIL PROTECTED]>
> To: "'M.V.Johnson'" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
> Sent: Friday, September 12, 2003 1:25 PM
> Subject: RE: Searching files
> 
> 
> > M.V.Johnson wrote:
> > > What is the easiest way to search a text file for the line "CLASS
> > > IC" 
> > 
> > $ grep -q 'CLASS IC' mytextfile && echo 'Found it!'
>
> I am familiar with grep in the shell. Will that grep line work in the
> context of a perl script? 

My answer was somewhat tongue-in-cheek. :~)

> 
> The code is looking at a directory full of .txt files. I want
> to parse a
> line in the .txt file only if the file does NOT contain the
> line "CLASS IC"
> 

It sounds like a two-step problem: 1) identify those files containing "CLASS
IC", and then 2) do some processing on those files. 

It's hard to beat something like grep -l for the first task. So you could do
something like:

   $ grep -l 'CLASS IC' *.txt | xargs myscript.pl

Or, if you want to wrap that into your perl script, then:

   chomp(@ARGS = `grep -l 'CLASS IC' *.txt`);

You can, of course search for the string using pure Perl code. Whether you
should depends on what the addtional processing you're doing is. What
exactly does "parse a line" mean? Is the line you want to parse the line
that contains 'CLASS IC' or a different line? Does 'CLASS IC' always appear
in the file above the line you want to parse (i.e. can you determine that
you are interested in parsing the line before you reach it?) Are you going
to be updating the file?

The perl equivalent of grep -l would be something like:

$ perl -nle 'print($ARGV), close ARGV, next if /CLASS IC/' *.txt

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to