sed not working properly with slash /

my testfile is

aspsun1:usasp000$cat testfile
open connection
put $TMPDIR/SUNIA.PJ080202.ENGRPTBZ.<OPERATOR>.133 <FILENAME>
quit

my problem statement is to replace the line with put command with 2 different lines a cd command and then put line.
from

put $TMPDIR/SUNIA.PJ080202.ENGRPTBZ.<OPERATOR>.133 <FILENAME>

to

cd $TMPDIR
put SUNIA.PJ080202.ENGRPTBZ.<OPERATOR>.133 <FILENAME>

for this i have used 4 variables

$echo $line
put $TMPDIR/SUNIA.PJ080202.ENGRPTBZ.<OPERATOR>.133 <FILENAME>
$TO_DIR=`echo $line |awk -f" " '{print $2}' |awk -F"/" '{$NF=""; print $0}'|sed 's/ /\//g' `
FILE_NAME=`echo $line |awk -f" " '{print $2}' |awk -F"/" '{print $NF}' `
REM_PART=`echo $line |awk -f" " '{$1="";print $0}'|awk -f" " '{$1="";print $0}'`
$echo $TO_DIR
$TMPDIR/
$echo $FILE_NAME
SUNIA.PJ080202.ENGRPTBZ.<OPERATOR>.133
$echo $REM_PART
<FILENAME>

but whenever i execute my command it just do not work, may be because of the '/' sign.

$sed 's/$line/cd $TO_DIR put $FILE_NAME $REM_PART/' testfile
open connection
put $TMPDIR/SUNIA.PJ080202.ENGRPTBZ.<OPERATOR>.133 <FILENAME>
quit

Please help !!

without going into details of your script which has a number of issues.....

sed "s#$line#cd $TO_DIR put $FILE_NAME $REM_PART#" testfile

You can do this with bash builtins:

# read a line into words[0...n]
while read -a words
do
  if [ "${words[0]}" = "put" ]
  then
    wrd=${words[1]}
    # split $wrd on the rightmost "/"
    dir=${wrd%/*}
    file=${wrd##*/}
    # if there were no "/" then $dir==$wrd and $wrd==$file
    if [ "$dir" != "$wrd" ]
    then
      echo "cd $dir"
      words[1]=$file
    fi
  fi
  echo "${words[@]}"
done