?
Solved

Variable in sed

Posted on 2004-11-29
4
Medium Priority
?
5,805 Views
Last Modified: 2008-01-02
Hi,

I've the problem with my shell script:
--------------
search_word=SEARCH
replace_word=REPLACE

sed 's/$search_word/$replace_word/g'  testfile
-------------
cat testfile
SEARCH

How do I make sed take variables $seach_word and $replace_word instead
of "SEARCH" and "REPLACE"?

0
Comment
Question by:wesly_chen
  • 2
4 Comments
 
LVL 38

Author Comment

by:wesly_chen
ID: 12700536
It's bourne or bash shell.
0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 2000 total points
ID: 12700637
Try double quotes:

sed "s/$search_word/$replace_word/g"  testfile
0
 
LVL 38

Author Comment

by:wesly_chen
ID: 12700717
You are right. I forget the difference between single quote and double quote.

Wesly
0
 

Expert Comment

by:askquestion_expert
ID: 20563215
I have a question here.
What if  the variable "replace_word" contains '/' . For ex: HOME variable in Linux

sed "s/$search_word/$HOME/g"  testfile

gives error "sed: -e expression #1, char 7: Unknown option to `s'"
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question