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
getting variable inside awk subin_bala Shell Programming and Scripting 1 06-05-2008 05:21 AM
Using a variable inside a file to cat another. MaestroRage UNIX for Dummies Questions & Answers 3 02-06-2008 02:17 PM
passing a variable inside a variable to a function KingVikram UNIX for Dummies Questions & Answers 2 01-14-2008 08:28 PM
How to replace variable inside the variable mani_um Shell Programming and Scripting 31 08-09-2007 11:56 PM
ksh: A part of variable A's name is inside of variable B, how to update A? pa3be Shell Programming and Scripting 4 03-30-2005 12:29 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 02-07-2007
Rock Rock is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 23
Using variable inside 'sed'

Dear all,


How can I amend the following command to use variable inside 'sed' ?

str=`sed -e 's/orig_string/${var}/g' ${sourcefile}`


Thanks,
Rocky
  #2 (permalink)  
Old 02-07-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Use double quotes

Code:
str=`sed -e "s/orig_string/${var}/g" ${sourcefile}`

  #3 (permalink)  
Old 11-23-2007
kofeyok kofeyok is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 1
Smile

Quote:
Originally Posted by anbu23 View Post
Use double quotes

Code:
str=`sed -e "s/orig_string/${var}/g" ${sourcefile}`
Also, if var is a path, then you'll need to use '\' before '/'. Sed substitutes first and then you still need to make sure you escape special characters such as '/'

Eg
set var="\/mydir\/path"
echo "my file /work/kofeyok/stuff/file.dat, r );" > testfile
sed -e "s/\/work\/kofeyok\/stuff/${var}/g" testfile > temp

temp should have
"my file /mydir/path/file.dat, r );"
without double quotes
  #4 (permalink)  
Old 11-23-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
set var="/mydir/path"
echo "my file /work/kofeyok/stuff/file.dat, r );" > testfile
sed -e "s#/work/kofeyok/stuff#${var}#g" testfile > temp

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