Replacing part of the sentence using echo and sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing part of the sentence using echo and sed
# 1  
Old 05-29-2013
Replacing part of the sentence using echo and sed

Hi,

Iam using ksh and trying to execute the following syntax to replace one word of the sentence with a new word. But somehow sed is not able to replace the old value with new value. Please let me know where Iam going wrong.

Sample Code :

-->
Code:
export line="VORTEX,abcdef"
export isname="VORTEX"
export f_new=`echo ${line} | sed -e 's/\"${isname}\"/IDEA/g'`
echo ""$f_new

-->

Expected Result :
Code:
IDEA,abcdef

Actual Result obtained :
Code:
VORTEX,abcdef


Last edited by Scrutinizer; 05-29-2013 at 02:02 PM.. Reason: code tags
# 2  
Old 05-29-2013
Code:
export f_new=`echo ${line} | sed -e "s/$isname/IDEA/g"`

This User Gave Thanks to Yoda For This Post:
# 3  
Old 05-29-2013
Hi Yoda,

Thanks :) It is working.
But what I didn't get is why single quotes is not working.

Not able to understand when to use single quoates and double quotes.
Please advise.
# 4  
Old 05-29-2013
Enclosing a referenced value in double quotes (" ... ") does not interfere with variable substitution.

This is called partial quoting, sometimes referred to as weak quoting.

Using single quotes (' ... ') causes the variable name to be used literally, and no substitution will take place.

This is full quoting, sometimes referred to as strong quoting.

So you have to always use double quotes or weak quoting if you want to reference a variable value.
This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to extract only relevant part from a sentence?

Hello All, I have a file with details such as below. How do i extract only the host and port ? eg: dbs.ads.com 1521 (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=dbs.ads.com)(PORT=1521))(CONNECT_DATA=(SID=vug)))... (5 Replies)
Discussion started by: JohnJacobChacko
5 Replies

2. Shell Programming and Scripting

Replacing echo with print

Hi everyone, I'm executing a shell script and one of the commands is creating a file with text via echo. However, if the text within echo has "\t" or similar, it automatically translates it into a TAB character, same goes for other special characters. I know that if I put another "\"... (7 Replies)
Discussion started by: demmel
7 Replies

3. UNIX for Dummies Questions & Answers

Replacing part of filename

Hi guys! I have quite a lot of files like all_10001_ct1212307460308.alf* and I want to get rid of the first number for all at once like: all_ct1212307460308.alf* How can I do this in the shell? (12 Replies)
Discussion started by: TimmyTiz
12 Replies

4. Shell Programming and Scripting

How to read in a part of an echo line?

Hello, I have a very basic script #!/usr/bin/ksh while print ' ' print ' 1. View Command History ' print ' 2. List files in current Directory ' read opt'?Enter Option> ' ;do if ;then fc -l fi # if ;then ls -la I want to... (10 Replies)
Discussion started by: Grueben
10 Replies

5. Shell Programming and Scripting

SED (or other) upper to lowercase, with first letter of first word in each sentence uppercase

The title pretty much defines the problem. I have text files that are all in caps. I would like to convert them to lowercase, but have the first letter of the first word in each sentence in uppercase. I already have SED on the server for fixing / tweaking text files, but I'm open to other... (5 Replies)
Discussion started by: dockline
5 Replies

6. Shell Programming and Scripting

Echo is replacing characters

Hi All, I'm using KSH and am writing a small script that counts the lines of various files in several folders under one root folder. Below is the script: #!/usr/bin/ksh common_path_in="/interface_in/rsc" file_out="/interface_in/rsc/record_count.csv" tot_rec_count=-1 act_rec_count=-1... (5 Replies)
Discussion started by: jagari
5 Replies

7. Shell Programming and Scripting

Replacing part of a pattern in sed

Hi I have a piece of xml that has a pattern like this <int>159</int><int>30</int> I want to find this pattern but only substitute the second part of the pattern to {rid1}. Is that possible in sed ? Thanks. ---------- Post updated at 12:10 PM ---------- Previous update was at 12:01 PM... (11 Replies)
Discussion started by: vnn
11 Replies

8. Shell Programming and Scripting

want to pass sentence with SED and CAT

I have to pass a sentence in a file, the specs are as: cat run | sed 's/SRT/'$8'/g' | sed 's/plength/68/g' | sed 's/stcol/'$5'/g' | sed 's/encol/'$6'/g' | sed 's/brdtype/'$1'/g' | sed 's/brdtxt/'$3'/g' | sed 's/demotxt/Total '$2'/g' | sed 's/bantxt/ban_'$7'/g' | sed 's/validcodes/'$4'/g' > runx... (1 Reply)
Discussion started by: patilrakesh1984
1 Replies

9. UNIX for Dummies Questions & Answers

Script to ask for a sentence and then count number of spaces in the sentence

Hi People, I need some Help to write a unix script that asks for a sentence to be typed out then with the sentence. Counts the number of spaces within the sentence and then echo's out "The Number Of Spaces In The Sentence is 4" as a example Thanks Danielle (12 Replies)
Discussion started by: charlie101208
12 Replies

10. Shell Programming and Scripting

Replacing the part of file name?

Hi All One of my script generate following files. These files has static TIMESTAMP 20080227. AccAdd_20080227_1000.dat AccBal_20080227_1000.dat Acc_20080227_1000.dat AccGrpMem_20080227_1000.dat AccToCust_20080227_1000.dat What i need to do is, once the file has been generated, it... (7 Replies)
Discussion started by: Amit.Sagpariya
7 Replies
Login or Register to Ask a Question