Using varible/varible substitution in Perl/sed Search & Replace

Hi,
I have a program that searches for a particular string patten. I am however having difficulty passing the varible $i (in a loop) as the string pattern to replace. Using either perl or sed search and replace statements, I get the same kinda result. For example, using the perl:

for i in `grep "#" $File`
do
perl -pe s/$i/Not-Implimented/' $File
done

I get no change. Furthermore if I replace the $i with "$i",
it simply writes the new string "Not-Implimented" on every line!

I get no change when using the "$i" variable when using the sed search and replace:
sed 's/"$i"/Not-Imp-lmplimented/g' $File

anyhelp would be appreciated.
Thanks
:o

here is a link to some thing that may help.

You could just as easily use sed instead of perl. Also, your match may be failing. It is possible that when you capture the value for $i that it is not matching properly.

Also I would try executing the sed/perl statement from the command line to see what it does there.

Here is my attempt. Here is my input file and the output after I execute the command line.

Every other line has a # in it...

/root> cat toddfile
# This is a test
This is a test
# This is a test
This is a test
# This is a test
This is a test
# This is a test
This is a test
# This is a test

Here is the execution of the command line and output...

root> sed -e 's/\#/Not-Implimented/' toddfile
Not-Implimented This is a test
This is a test
Not-Implimented This is a test
This is a test
Not-Implimented This is a test
This is a test
Not-Implimented This is a test
This is a test
Not-Implimented This is a test

Now that I look at your script, I think that the "grep" is the problem.

One thing that I noticed is that the problem is if the line returned by grep has spaces in it. Then the list for i is broken down even further.

Example:

I took this simple script that I created for debugging and got the seperated output:

for i in `grep "#" source.txt`
  do
    echo "i is ${i}."
  done

Output:

FreeBSD:joeuser:/home/joeuser/sample $ ./imp.sh
i is #.
i is This.
i is is.
i is a.
i is test.
i is #.
i is This.
i is is.
i is a.
i is test.
i is #.
i is This.
i is is.
i is a.
i is test.
i is #.
i is This.
i is is.
i is a.
i is test.
i is #.
i is This.
i is is.
i is a.
i is test.

Is there any way to reset the delimiter for the for loop in a shell? I don't remember off the top of my balding head.

Thank you both for your input. Yes, I considered that the grep will execute an iretation of the loop as it encounters a whitespace in the stream following '#', and subsquently, ensured the '#' occurs as one string only (therfore echo $i = #Normal-String).
The reference of '$i' seems to work well in the sed statement, so thanks again. :slight_smile: