One possibility If the files your working with aren't too huge:

Open the file, un-define the input record separator and read the whole file
into a variable.  With a substitution regex, replace the yyyyy with wwwww.
Reopen the file for writing and replace the original file with the new data.


        $/ = undef;
        $fileName = 'file1.txt';
        open(INFILE, "$fileName");
        $file = <INFILE>;
        close(INFILE);
        $file =~ s/(.*?\nINFILE
'(.)\2*)((.)\4*)((.)\6*\.dat.*)'/$1wwwww$5/s;
        open(OUTFILE, ">$fileName");
        print OUTFILE $file;
        close(OUTFILE);

In the regex, the .*?\n at the beginning will match the first line.  The .*
at the end matches the end of the file.  The stuff in between is the key.
Back-references are used to match repeating arbitrary characters.  For
example, the \2* matches zero or more of whatever character was matched by
the preceding dot.  This is done three times for the three repeating
characters.  I hardcoded the 'wwwww' into the regex, but you could also use
a variable if you want to be able to change the replacement string on the
fly.

For multiple files in the same directory, you could read the directory to
get the file names and loop around the above code for each file name

        opendir(DIR, "directory name");
        @fileNames = grep {/file\d+\.txt/} readdir(DIR);
        $/ = undef;     
        foreach $fileName (@fileNames) {
                open(INFILE, "$fileName");
                $file = <INFILE>;
                close(INFILE);
                $file =~ s/(.*?\nINFILE
'(.)\2*)((.)\4*)((.)\6*\.dat.*)'/$1wwwww$5/s;
                open(OUTFILE, ">$fileName");
                print OUTFILE $file;
                close(OUTFILE);
        }
                
Note: I didn't test all of this code, but the regex should work (and I'm
guessing that is what you were most interested in).


wantor


> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, April 27, 2000 12:10 PM
> To: Perl-Win32-Users Mailing List
> Subject: Help with find and replace on a specific line
> 
> 
> Hi:
> 
> Suppose I have a directory with files file1.txt, file2.txt, 
> ..., filen.txt
> 
> Suppose the the 2nd line is always
> 
> INFILE 'xxxxxyyyyyyzzzzzz.dat'
> 
> where x y and z repeat differently in every file.  It is 
> always the 2nd
> line in each file.
> 
> How do I replace yyyy with wwww?  TIA?
> 
> Satish
> 
> 
> ---
> You are currently subscribed to perl-win32-users as: 
> [EMAIL PROTECTED]
> To unsubscribe, forward this message to
>          [EMAIL PROTECTED]
> For non-automated Mailing List support, send email to  
>          [EMAIL PROTECTED]
> 

---
You are currently subscribed to perl-win32-users as: [archive@jab.org]
To unsubscribe, forward this message to
         [EMAIL PROTECTED]
For non-automated Mailing List support, send email to  
         [EMAIL PROTECTED]

Reply via email to