Special characters in a bash variable in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Special characters in a bash variable in sed
# 1  
Old 04-12-2009
Special characters in a bash variable in sed

Hello,

I am trying the following:
echo __CHANGEME__ >> testfile
VAR1="&&&"
sed -i "s|__CHANGEME__|${VAR1}|" testfile
cat testfile

This results in testfile containing
__CHANGEME____CHANGEME____CHANGEME__

Whereas I want it to result in
&&&

I understand that if
VAR1="\&\&\&"
then it would work.

But this is a simplified version of my script. In my actual script VAR1 is coming in as a user input/randomly generated ASCII string.

I also understand that I can write some code to insert escape characters in "VAR1". But I am hoping that there a more intelligent way to do this using sed only. For some reason '&' is the only special character that is causing problems.

Thanks in advance for your input.

Last edited by linuxnewbeee; 04-12-2009 at 05:48 AM..
# 2  
Old 04-12-2009
Hi,
try using perl

perl -pi -e 's/__CHANGEME__/'$var1'/' testfile

Bye
# 3  
Old 04-12-2009
Hi Sauron,

Your suggestion works perfectly. However since I am trying to learn sed, I would still appreciate if some sed experts could shed some light on this. Is it possible to do this using sed, or is this just a sed limitation that I should make a mental note of for the future.

Thanks.
# 4  
Old 04-13-2009
You could try to escape your replacement string with backslashes before:

Code:
echo __CHANGEME__ >> testfile
print - "Enter VAR1:" ; read VAR1
VAR1="$(print - "$VAR1" | sed 's/[&=\|]/\&/g')"
sed -i 's|__CHANGEME__|'"${VAR1}"'|' testfile
cat testfile

Btw., i have changed your quotations a bit to make the script more robust. I have included only four metacharacters ("\", "&", "=", "|") to be escaped but you can easily expand the expression to make it more general. In any case you will have to escape your delimiting char, which is "|" here.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Special Characters BASH/UNIX term support

Trying to run this command: find /Volumes/Archive/ -type f -name "Icon" and get /Volumes/Archive//New Business and Marketing/2010 /Creative/Image Library/Stuff for Sean/Cafe Heineken/logo_Café Heineken_03.jpg: No such file or directory due to the accent on the filename. Is there a way around... (2 Replies)
Discussion started by: kostas123334
2 Replies

2. Shell Programming and Scripting

sed in a while loop with special characters

I have the foolowing data file: File1 <p name="A">5004</p> <p name="B">5004</p> <p name="C">5004</p> <p name="A">15004</p> <p name="B">15004</p> <p name="C">15004</p> In a while loop using sed (100 of line need to be replace), I need the output to File3:... (2 Replies)
Discussion started by: bobo
2 Replies

3. Shell Programming and Scripting

Parsing special characters from variable commands

Hi, I am fairly new to unix scripting and recently tasked with some reporting scripts. The reporting checks several batch jobs and this is quite iterative. Now I am trying to minimize script effort and maximize reusability as there are only slight nuances in the repetitive tasks. For... (3 Replies)
Discussion started by: joeniks
3 Replies

4. Shell Programming and Scripting

Trouble with sed and substituting a string with special characters in variable

Hey guys, I know that title is a mouthful - I'll try to better explain my struggles a little better... What I'm trying to do is: 1. Query a db and output to a file, a list of column data. 2. Then, for each line in this file, repeat these values but wrap them with: ITEM{ ... (3 Replies)
Discussion started by: ampsys
3 Replies

5. UNIX for Dummies Questions & Answers

Environment Variable with Special characters

Hello all, I am facing with a problem of invoking an environment variable. If I use this command : /bin/ls -lt FILE_NM_?(20120515|20120516)??????_sas.sig | head -n1 | awk '{print $9}' It perfectly outputs the desired result. FILE_NM_20120516000000_sas.sig But if I do this:... (8 Replies)
Discussion started by: sethmj
8 Replies

6. Shell Programming and Scripting

Sed failing for variable with special characters

This has been covered many times earlier but couldnt figure the issue myself. Can you please advise whats wrong on the below code I have a variable with special character ($) and am using that variable to replace another variable in file but however sed is failing to parse it correctly ... (7 Replies)
Discussion started by: sasiharitha
7 Replies

7. Shell Programming and Scripting

SED with Special characters

Hello All Seeking the right one SED command. My attempt is: From orginal.txt by SED to target.txt sed -i "/('outbound-callerid/a\$ext->add($context, $exten, '', new ext_SipAddHeader('P-Preferred-Identity', '<sip:${CALLERID(nummer)}@carrier.com>'));" orginal.txtWhat am make wrong?:wall: ... (5 Replies)
Discussion started by: mdbinder
5 Replies

8. Shell Programming and Scripting

sed with many special characters

I started with this: counter1=1 cp file.txt file_${counter1}.tmp while read name1 do echo $name1 counter2=`expr $counter1 + 1` sed /'${name1}'/d file_${counter1}.txt > file_${counter2}.txt counter1=`expr $counter1 + 1` done < source.txtsource.txt contains the... (1 Reply)
Discussion started by: lakanino
1 Replies

9. Shell Programming and Scripting

sed with special characters

Hi, I am reading a file (GC_JAR.log) which has entries like: 511725.629, 0.1122672 secs] 525268.975, 0.1240036 secs] 527181.835, 0.2068215 secs] 527914.287, 0.2884801 secs] 528457.134, 0.2548725 secs] I want to replace all the entries of "secs]" with just "secs" Thus, the output... (4 Replies)
Discussion started by: itzz.me
4 Replies

10. Shell Programming and Scripting

Variable Manimpulation - special characters

Hello- I have a variables that contains a string like this usr/pass@SCHEMA I want to extract the usr/pass part and ignore the SCHEMA part, I tried to use this ${dbconn%%@} and apparently it will not work because @ is a special character. I tried \@ and still no go. Any idea how to solve... (1 Reply)
Discussion started by: Nomaad
1 Replies
Login or Register to Ask a Question