The UNIX and Linux Forums  

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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find and Replace NycUnxer UNIX for Dummies Questions & Answers 4 03-06-2008 12:20 AM
find replace dbsurf Shell Programming and Scripting 2 01-25-2008 08:39 AM
find and replace mahabunta UNIX for Dummies Questions & Answers 7 09-21-2006 01:05 PM
find and replace vikas_j@hotmail UNIX for Dummies Questions & Answers 3 02-25-2002 05:41 PM
Find & Replace gagansharma Shell Programming and Scripting 3 11-27-2001 04:17 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #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

  #2 (permalink)  
Old 05-27-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,372
Quote:
Originally Posted by allrise123 View Post
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

Variables are not expanded inside single quotes; use double quotes:


Code:
echo ${line} | sed "s|allrise|hello $name|g" >>$NEW_FILE

Quote:

Code:
     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

Who don't you do:


Code:
sed "s/allrise/hello $name/" "$FILE" > "$NEWFILE"

There's no need for a loop.
  #3 (permalink)  
Old 05-27-2009
allrise123 allrise123 is offline
Registered User
  
 

Join Date: May 2009
Posts: 9
thanks for reply..

Actually this is not my actual file... this is just dummy file which i created just to ask my query... anyways i will try your suggestion..
  #4 (permalink)  
Old 06-06-2009
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,893
Duplicate of find and replace query

(sock puppet)
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0