develooper Front page | perl.beginners | Postings from February 2009

replace text in file

Thread Next
From:
lemba
Date:
February 25, 2009 17:02
Subject:
replace text in file
Message ID:
49A5EA41.8090405@sbcglobal.net
Hi All,

I'm trying to replace some text in file. Below is my script. It 
basically makes a copy of line with changes. How can I replace pattern 
in the uxix sed way?

use strict;
use warnings;
use Cwd;
use Win32;
use File::Path;
use File::Find;
use File::Basename;

@ARGV = Win32::GetCwd() unless @ARGV;

my @source;
find (\&findSource, $ARGV[0]);

foreach ( @source )
{
        open SOURCE, "+<", $_ or die "Error: $!\n";
        my @file = <SOURCE>;

        seek SOURCE,0,0;
       
        foreach my $file (@file)
        {
                if ( $file =~ /STARBUCKS.*RESTAURANT/i )
                {                 
                   $file =~ s/RESTAURANT/CAFE/g;                 
                   print SOURCE $file;
                }
        }
        close SOURCE;
}

Example of my original file:
STARBUCKS|RESTAURANT
JACK IN THE BOX|RESTAURANT
STARBUCKS|RESTAURANT
MC DONALDS|RESTAURANT

after replacement it should be like this:
STARBUCKS|CAFE
JACK IN THE BOX|RESTAURANT
STARBUCKS|CAFE
MC DONALDS|RESTAURANT

Thanks in advance,
Vladimir


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About