Please help re: reg exp from command line... 
Author Message
 Please help re: reg exp from command line...

Hi

I am awaiting my programming perl book and am unable to find out how
I need to do this from perlfaq6.  This question is more on the syntax
end.  Can someone please assist me?

This is what I want to do:
        -From the command line search on a regular expression.. say
         diabetes.. in all of the .html's in the current directory.
        -Once I find a match on the regular expression diabetes I want
         to print the name of the file AND the title name(sans </TITLE>)
         to STDOUT

I believe I need to put the reg exp in delimiters.. /diabetes/ with
single quotes around it.  Also in my $PATH is the current working
directory .

What would the correct syntax be at the command line with applicable
switches?
    #perl  what switches  regexp  filename?

Thank you for any help.  

Have a good day.

Pam Polk



Tue, 11 Jan 2000 03:00:00 GMT  
 Please help re: reg exp from command line...


Quote:
>This is what I want to do:
>    -From the command line search on a regular expression.. say
>     diabetes.. in all of the .html's in the current directory.
>    -Once I find a match on the regular expression diabetes I want
>     to print the name of the file AND the title name(sans </TITLE>)
>     to STDOUT

I'll assume you're using a Unix system; if not ... I'm not sure what
the command line syntax would be.

I use the find program to print out the names of all the .html files,
then I run perl on those names, for example:

$ perl -e 'script line 1;' -e 'script line 2' `find . -name '*.html' -print`

You'll often want the -n switch to perl - it makes perl read through
all of the files named on the command line (so you don't have to

Your request is a bit complicated - it might work better if you put the
program in a file, not just write it on the command line.  It's
certainly easier to modify that way.  If you insist on calling it from
the command line, you can just put each line of the file into its own
-e '...' flag.

This might get you started:

#!/usr/bin/perl -w
#
# I didn't test this code!
#

use strict;

# pass the regexp as the first arg to your program
my $pattern = shift;

# loop through the file names listed on command line
my $fname;
while ($fname = shift) {
    open FH, $fname or do { warn "can't open $fname: $!\n"; next; };

    my ($title, $found);
    while (<FH>) {
        # look for title and remember the title text
        m:<TITLE>(.*)</TITLE>:i and do {
            $title = $1;
            last if $found;
        };

        # look for pattern
        m:$pattern: and do {
            $found = 1;
            last if defined $title;
        };
    }

    close FH;

    if ($found) {
        print "$title ($fname)\n";
    }

Quote:
}

exit 0;
__END__

If you've put this into a file called "findpattern" and put that file
into a directory in your PATH and made that file executable and
readable, then you could do from the command line:

$ findpattern '(?i)diabet(?:es|ic)' `find . -name '*.html' -print`

That would look for any file containing diabetes or diabetic, case
insensitive.

This might not be exactly what you want.  Hopefully it will get you
going.

Tim



Fri, 21 Jan 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. reg exp help, please

2. SIMPLE REG-EXP Please Help!

3. SIMPLE REG-EXP Please Help!

4. multi-line reg exp deletion

5. Line Reg Exp

6. Help with reg.exp.

7. NEED HELP IN CREATING REG EXP

8. Reg exp help

9. Stumped by Reg Exp Problem - help??

10. reg exp help

11. help with removing leading zeros with reg-exp

12. Reg Exp request for help

 

 
Powered by phpBB® Forum Software