On Saturday 06 September 2003 17:53, Pandey Rajeev-A19514 wrote :
: How do I search replace text in a file from a perl script.
: i.e. by opening the file in write mode and then do a search
: replace.
You can do what you want via the regular expression substitution
operator "s///" in a "while" loop.
<code>
#!/usr/bin/perl
# These two aren't really necessary for this script, but
# they are *generally* considered to be good form when
# writing and debugging. More about them in the Perl docs.
use strict;
use warnings;
my $word_to_replace = 'Abdel';
my $word_to_insert = 'Aisha';
while (<>) { # This reads in each line of the
# file, one at a time.
s/\b$word_to_replace\b/$word_to_insert/g;
# The \b means "word boundary" in a regular expression
print;
}
</code>
Use it like:
perl thisprogram.pl textfile.txt ( > outfile.txt)
This will replace all instances of "Abdel" in isolation, i.e. not
"Abdelraman" with "Aisha".
You can have a look at the following link for more information:
http://www.perlmonks.org/index.pl?node=Tutorials
or use the built-in Perl documentation by typing
perldoc perltoc
at the command line for the Perl Documentation table of contents.
Hope that helps,
Damon "allolex" Davison
--
Damon Allen Davison
http://allolex.freeshell.org
"A UNIX life is hard."
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]