On Thu, 19 Sep 2002, Ramprasad A Padmanabhan wrote:
> Hi All
>
> I am writing a script that will replace all instance of a particular
> word another in a file
>
> The only problem is that I want to match only whole words
> eg If $str="the there thee that the";
>
> I want to change to $str="abc there thee that abc";
>
>
> $srcstr = 'the'
> $dest = 'abc'
>
> $str=~s/([^\w\_\-\.])\Q$srcstr\E([^\w\_\-\.])/$1$dest$2/g;
> this doesnt seem to work
>
> I will enclose my entire script here
> #!/usr/bin/perl
> #
> # This will work as sedfiles diff being it replaces only whole words
> # A word will mean only char seq containing \w \_ \- \.
> # No Regex is allowed in srcstring
> #
>
> die "Usage $0 <srcstring> <deststring> <file1>..<file2>\n" unless (@ARGV
> > 2 );
>
> $srcstr = shift(@ARGV);
> $deststr = shift(@ARGV);
> {
> local $^I = '~';
> while (<>) {
>
> # This does not work
> s/([^\w\_\-\.])\Q$srcstr\E([^\w\_\-\.])/$1$deststr$2/g;
>
> # This works but not what I need
> #s/\Q$srcstr\E/$1$deststr$2/g;
What are the $1 and $2 here for, you are not capturing anything here.
If you want to replcae all (whole) occurences of $srcstr by $deststr
this should work
s/\b\Q$srcstr\E\b/$deststr/g;
>
> print;
> }
> }
>
>
>
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]