How to pass a variable to a sed command in a csh script?

Unix & Sed noob here!

I have a requirement where I need to put a suffix ",waive" in case a line in a report contains the matching string.The matching string is part of another file "waive.txt". I am also trying to perform these operations on a copy of the main report.

I am putting an example below

Here is how the report looks like:

i love apple_tart as a desert
banana is full of potassium and iron
there are so many helath benefits of apple eating 
the king of fruit is mango 
there are apple_pie of many variety

Here is how waive.txt looks like:

apple_pie
banana

This is my expeted output which I want in the same file :

i love apple_tart as a desert
banana is full of potassium and iron,waive
there are so many helath benefits of apple eating 
the king of fruit is mango 
there are apple_pie of many variety,waive

This is the script I tried but it is giving Illegal variable name Error

#!/bin/csh -f
if (-r fruits_bkup.txt) then
  rm fruits_bkup.txt
endif

yes|cp fruits.txt fruits_bkup.txt
foreach word ("`cat waiver.txt`")
  sed -i "/${word}/s/$/ ,waived/" fruits_bkup.txt 
end  

If I replace the double quotes with a single quotes in the sed command, nothing happens to the report.

Thanks!

PS: work requires me to work with the csh shell.

Welcome!
The double-quotes must be around the ${word} so the shell substitutes it. But the other $ should stay as is.
Two methods of quoting it:

sed -i "/${word}/s/\$/ ,waived/" fruits_bkup.txt

The backslash escape works within double quotes with some characters (the ones that are still special).

sed -i "/${word}"'/s/$/ ,waived/' fruits_bkup.txt

The shell sees concatenated "string" and 'string' where the second one quotes all characters, even the dollar.
As usual the shell dequotes the (composed) argument before passing it to the sed.

If you fear interaciveness then better go for
\cp
that inhibits an eventual alias.
Analogue: \rm

Thanks! your solution works. Handling double quotes and single quotes is a tricky affair in sed.

A follow up question:

If the string to be matched has special characters like [, ], /, \, &, $ etc then, how do we take care of this in our command?

Note that I was talking about the shell.
The sed script was embedded in the shell code.

] [ \ ^ $ are special characters that sed uses in a /BRE/ search or s/BRE/string/ substitution (BRE=BasicRegularExpression). The only method is to backslash escape them, then they lose their special meaning.
\] \[ \\ \^ \$

A / is not special in a BRE but collides with the / delimiters, so must be escaped as well.
If there are many / then it makes sense to use another delimiter, say a #
\#BRE#
s#BRE#string#
Now the BRE may have normal / but not #

A & is not special in a BRE.
But it is special in the sed substitution string i.e. the right side in a
s/BRE/string/
There it must be backslash escaped to lose its special meaning.

another possible option to sed ...
Using updated copies of fruits.txt and waive.txt

cat waive.txt 
apple_pie
Kumquat
(kumquat)
[pineapple]
pine nuts
banana
(ugli)
orange
"Mango"
"MANGO"
mangO
cat fruits.txt 
i love apple_tart as a desert
banana is full of potassium and iron
the prince of fruit is the (ugli)
the baron of fruit is the ugli but only in UK
there are so many banana health benefits of banana or apple eating 
the king of fruit is mango 
the old lord of fruit is "MANGO"
nuts of all type are to be consumed in small amounts, pine nuts especially
the rogue fruits are pineapple and any berry
not to be confused by MANGO
the queen of fruit is the [kumquat] 
there are apple_pie of many variety
cat script.awk 
FNR==NR{ kiwds[x++]=$0; next } #store the list of keywords to search
{ 
	for( ki in kiwds ) # see if ANY them are on the current line
		if ( index($0,kiwds[ki]) ) { $0=$0",waive"; break }
}1
awk -f script.awk waive.txt fruits.txt 
i love apple_tart as a desert
banana is full of potassium and iron,waive
the prince of fruit is the (ugli),waive
the baron of fruit is the ugli but only in UK
there are so many banana health benefits of banana or apple eating ,waive
the king of fruit is mango 
the old lord of fruit is "MANGO",waive
nuts of all type are to be consumed in small amounts, pine nuts especially,waive
the rogue fruits are pineapple and any berry
not to be confused by MANGO
the queen of fruit is the [kumquat] 
there are apple_pie of many variety,waive

@kumaraditya,
IF you had the luxury to switch from old/ugly csh to a more modern bash.
kuma.sh:

#!/bin/bash

while read word junk
do
  sed "/${word@E}/s/\$/ ,waived/" fruits_bkup.txt
done < waiver.txt

kuma.sh | grep waived would have yielded - sample files taken from @munkeHoller's post as a test harness:

there are apple_pie of many variety ,waived
i love apple_tart as a desert ,waived
banana is full of potassium and iron ,waived
the prince of fruit is the (ugli) ,waived
the baron of fruit is the ugli but only in UK ,waived
there are so many banana health benefits of banana or apple eating  ,waived
the king of fruit is mango  ,waived
the old lord of fruit is "MANGO" ,waived
nuts of all type are to be consumed in small amounts, pine nuts especially ,waived
the rogue fruits are pineapple and any berry ,waived
not to be confused by MANGO ,waived
the queen of fruit is the [kumquat]  ,waived
there are apple_pie of many variety ,waived
nuts of all type are to be consumed in small amounts, pine nuts especially ,waived
the rogue fruits are pineapple and any berry ,waived
banana is full of potassium and iron ,waived
there are so many banana health benefits of banana or apple eating  ,waived
the prince of fruit is the (ugli) ,waived
the old lord of fruit is "MANGO" ,waived

What's the @E ?

from man bash under EXPANSION:

       ${parameter@operator}
              Parameter transformation.  The expansion is either a transforma‐
              tion of the value of parameter or  information  about  parameter
              itself,  depending on the value of operator.  Each operator is a
              single letter:

              Q      The expansion is a string that is the value of  parameter
                     quoted in a format that can be reused as input.
              E      The  expansion is a string that is the value of parameter
                     with backslash escape  sequences  expanded  as  with  the
                     $'...' quoting mechansim.
              P      The expansion is a string that is the result of expanding
                     the value of parameter as if it were a prompt string (see
                     PROMPTING below).
              A      The  expansion  is  a string in the form of an assignment
                     statement or declare command  that,  if  evaluated,  will
                     recreate parameter with its attributes and value.
              a      The  expansion is a string consisting of flag values rep‐
                     resenting parameter's attributes.

              If parameter is @ or *, the operation is applied to  each  posi‐
              tional  parameter  in  turn,  and the expansion is the resultant
              list.  If parameter is an array variable subscripted with  @  or
              *,  the case modification operation is applied to each member of
              the array in turn, and the expansion is the resultant list.

              The result of the expansion is subject  to  word  splitting  and
              pathname expansion as described below.

A new feature?
The bash reference
even describes @u @U @L
They are redundant with ^ ^^ ,, and I wonder if they forgot @l alias ,
Feature creep?

@kumaraditya,
it seems that the bash's ${var@E} doesn't do what I though it was supposed to do - thanks for @munkeHoller for cross-validation.
I needed to take the printf '%q' path do the proper quoting/escaping of the bash's/shell variable.

#!/bin/bash
#set -x

while read word
do
  printf -v wordE '%q' "${word}"
  sed "/${wordE}/s/\$/ ,waived/" fruits_bkup.txt
done < waiver.txt

kuma.sh | grep waived would have yielded - sample files taken from @munkeHoller's post as a test harness:

there are apple_pie of many variety ,waived
the queen of fruit is the [kumquat]  ,waived
nuts of all type are to be consumed in small amounts, pine nuts especially ,waived
banana is full of potassium and iron ,waived
there are so many banana health benefits of banana or apple eating  ,waived
the prince of fruit is the (ugli) ,waived
the baron of fruit is the ugli but only in UK ,waived
the old lord of fruit is "MANGO" ,waived

I've cross-checked the output for anything pine for BOTH of the input files and the script returns what it's supposed to.
You'll have to do your own validation.

How about ${var@Q} ?

Nope, didn't help.