Use variables with double quotes sed -i


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use variables with double quotes sed -i
# 1  
Old 02-05-2010
Use variables with double quotes sed -i

I have the following line of code:

Code:
sed -i "/MatchText/ s/${tgrepLine}/${tNewLine}/" filename.output

filename.output contains this:

Code:
blablabla
PATH=".:/home/root/bin/:/usr/local/bin/"
blablabla

Variable ${tgrepLine} contains:
PATH=".:/home/root/bin/:/usr/local/bin/"
Variable ${tNewLine} contains:
PATH="/home/user/bin/:.:/home/root/bin/:/usr/local/bin/"

As you can see, both variables contains " (double quotes)
If I run the 'sed -i' command, I get an error.
I cant use double quotes within the double quotes of the 'sed' command...

The sed command would look like this:

Code:
sed -i "/MatchText/ s/PATH=".:/home/root/bin/:/usr/local/bin/"/PATH="/home/user/bin/:.:/home/root/bin/:/usr/local/bin/"/" filename.output

I must find a solution to this problem. Do I need to use the double quotes in both commands??

Thanks in advance!
# 2  
Old 02-05-2010
Hi.

The quotes aren't really a problem, if you escape them. In this case you can use single outer quotes. Also, your path contains /, which is "confusing" sed.

Change the separator to something else:

Code:

sed -i '/MatchText/ s@PATH=".:/home/root/bin/:/usr/local/bin/"@PATH="/home/user/bin/:.:/home/root/bin/:/usr/local/bin/"@' filename.output

# 3  
Old 02-05-2010
Tried it, but still nothing.
Tried this one:
sed -i s_${tgrepLine}_${tNewLine}_ filename.output

But than i got the following error:
sed: -e expression #1, char 38: unknown option to `s'

---------- Post updated at 11:54 AM ---------- Previous update was at 11:51 AM ----------

Quote:
Originally Posted by scottn
Hi.

The quotes aren't really a problem, if you escape them. In this case you can use single outer quotes. Also, your path contains /, which is "confusing" sed.

Change the separator to something else:

Code:

sed -i '/MatchText/ s@PATH=".:/home/root/bin/:/usr/local/bin/"@PATH="/home/user/bin/:.:/home/root/bin/:/usr/local/bin/"@' filename.output


Can I use single quotes?
If I use single quotes, the variable names are just displayed as 'variable names' and not with their content.
# 4  
Old 02-05-2010
I don't think using _ is a good candidate for a separator.

Code:
$ cat file1
PATH=".:/home/root/bin/:/usr/local/bin/"

$ echo $X
PATH=".:/home/root/bin/:/usr/local/bin/"

$ echo $Y
PATH="/home/user/bin/:.:/home/root/bin/:/usr/local/bin/"

$ sed "s@$X@$Y@" file1
PATH="/home/user/bin/:.:/home/root/bin/:/usr/local/bin/"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed command to replace consecutive double quotes

I need to replace consecutive double quotes in a csv file, the data in the file is enclosed in double quotes but there are some places where the quotes are repeating Example is below Incoming data is : "Pacific Region"|"PNG"|"Jimmy""|""| Need output as: "Pacific... (10 Replies)
Discussion started by: abhilashnair
10 Replies

2. Shell Programming and Scripting

sed add double quotes and comma

Hi, 00000119EEEC3F25 feedoor 20171103 0000011A4F152077 feedard 20171024 00000191FA295F61 feedzipperhola 20171023 00000213C57BB856 feedriodapple 20171005 0000025F778EF9D5 joobakoolrk 20171004 I needed the result as: "00000119EEEC3F25", "feedoor", ... (9 Replies)
Discussion started by: ashokvpp
9 Replies

3. Shell Programming and Scripting

Using sed and double quotes

I am working on a Raspberry Pi and using sed to update the SSID and password but the config file requires the SSID and PSK to both be in double quotes. Below is the code I am using but I cannot figure out how to include the double quotes. script: #!/bin/bash read -p "Please specify the... (2 Replies)
Discussion started by: NickCostas
2 Replies

4. Shell Programming and Scripting

Replace Double quotes within double quotes in a column with space while loading a CSV file

Hi All, I'm unable to load the data using sql loader where there are double quotes within the double quotes As these are optionally enclosed by double quotes. Sample Data : "221100",138.00,"D","0019/1477","44012075","49938","49938/15043000","Television - 22" Refurbished - Airwave","Supply... (6 Replies)
Discussion started by: mlavanya
6 Replies

5. Shell Programming and Scripting

sed command to replace string that contain blackslash,double quotes

Hi All, I have been trying to replace a string using the sed command string value contain blackslash and double quotes. I am not a expert writer of unix script but do try not to ask question. I have almost given up. Hope you all can give me some suggestion I want to replace a place string... (6 Replies)
Discussion started by: thanush9sep
6 Replies

6. Shell Programming and Scripting

Complex bash/sed, variables and nested quotes

Ok, this one isn't for everybody, it's pretty tough and I've spent a good deal of time on it without figuring it out yet. Can anybody get this script to work: #!/bin/bash cq_fname="%let outputfile="/user/cq_"$1".csv";" sed "29s/.*/\"$cq_fname\"/" file1.sas >... (3 Replies)
Discussion started by: nocloud
3 Replies

7. Shell Programming and Scripting

Replace double double quotes using AWK/SED

Hi, I have data as "01/22/97-"aaaaaaaaaaaaaaaaa""aaa""aabbbbbbbbcccccc""zbcd""dddddddddeeeeeeeeefffffff" I want to remove only the Consequitive double quotes and not the one which occurs single. My O/P must be ... (2 Replies)
Discussion started by: Bhuvaneswari
2 Replies

8. Shell Programming and Scripting

sed and double quotes

I have not seen anything yet in your forum to answer this problem. Here is my sed command. I want to change Build Apache module" off to Build Apache module" on This is what I am trying. sed "s_Build Apache module\" off_Build Apache module\" on_" /usr/ports/lang/php5/Makefile... (9 Replies)
Discussion started by: triumdh
9 Replies

9. Shell Programming and Scripting

sed removing comma inside double quotes

I have a csv file with lines like the followings 123456,"ABC CO., LTD","XXX" 789012,"DEF LIMITED", "XXX" before I bcp this file to database, the comma in "CO.," need to be removed first. My script is cat <filename> | sed 's/"CO.,"/"CO."/g' but it doesn't work. Can anyone here able to... (2 Replies)
Discussion started by: joanneho
2 Replies

10. AIX

how to pass variables surrounded in double quotes to awk?

Hi, I'm making progress on this but hung up on one last detail. I'd like to use AWK to pass the system date and time(among other things) to the first line of a file. Here's what I have: BEGIN {TOTALPP = 0;FREEPP=0;USEDPP=0;print "LPAR NAME:",lpar,"DATE:",tdate } I call AWK with the... (4 Replies)
Discussion started by: cruiser
4 Replies
Login or Register to Ask a Question