File processing on perl

Hey everyone ...

I wanted to process the contents of a file, as in modify its contents. whats the best way to do it on perl? In more detail I hav to go through the contents of the file, match patterns n then modify the contents of the same file depending on the matching results. Any help is welcome.

Thanks n regards,
Garric

Hi,

This may help you.

1. open the file with fopen.
2. Read the contents line by line.
3. match the string with your pattern and repace accordingly.
4. Write line in another file.
5. remove new file with previous one.

testfile.txt :

About Apple
Apple is a fruit
I like Apple Juice

Code

open(INFILE,"./testfile.txt")||die ("Cannot open testfile.txt");
open(OUTFILE,">./orange.txt")||die ("Cannot open orange.txt");
while($str=<INFILE>)
{
$str =~ s/Apple/Orange/g;
print OUTFILE $str;
}
close(INFILE);
close(OUTFILE);

Orange.txt

About Orange
Orange is a fruit
I like Orange Juice