The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 05-27-2009
allrise123 allrise123 is offline
Registered User
  
 

Join Date: May 2009
Posts: 9
find and replace problem

hi guys!!!

i am writing a script in which i take an input from user and find it in a file and replace it.
My input file looks like
Code:
hi
what your name?
allrise
my code looks is
Code:
echo "Enter the name"
read name
FILE="/opt/name.txt"
NEW_FILE="/opt/new_name.txt"
exec 0<$FILE
    while read line
    do
     if [ -n "`echo ${line} | grep 'allrise'`" ]
     then
              echo ${line} | sed 's|allrise|hello $name|g' >>$NEW_FILE
     else
             echo $line >>$NEW_FILE
     fi
     done <$FILE
When i run my script, it ask me for name suppose i gave "john"... but the output comes as shown below
Code:
hi
what your name?
hello $name
so, can anyone explain how i can get proper output which should be.
Code:
hi
what your name?
hello john