Sed failing for variable with special characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed failing for variable with special characters
# 1  
Old 11-20-2011
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


Code:
Code:
PASSWORD1=Mypm@123
PASSWORD_NEW=$(echo $PASSWORD1 | sed -e 's/\(\.\|\/\|\*\|\[\|\]\|\\\)/\\&/g')
sed  -i "s/PASSWORD_TEXT/"$PASSWORD_NEW"/g"  credentials.txt


Last edited by pludi; 11-20-2011 at 03:08 PM..
# 2  
Old 11-20-2011
You need to provide actual data for us to analyze. Your input and desired output will be helpful.
BTW the last sed statement doesn't need " around PASSWORD_NEW

--ahamed
# 3  
Old 11-20-2011
Here is the actual code and data
As mentioned below, the variable value isn't getting parsed correctly and instead of $ its taking the variable name while doing sed. Can you please help on what might be wrong?

credentials.template
installationHome=INSTALLATION_HOME
password=DEPLOYMGR_ADMIN_PASSWORD

Input Values received in script:
DEPLOYMGR_ADMIN_PASSWORD=Mypm@123
INSTALLATION_HOME=/opt/software

Script
sed -e "s:INSTALLATION_HOME:$INSTALLATION_HOME:g" credentials.template credentials.txt
sed -i "s/DEPLOYMGR_ADMIN_PASSWORD/$DEPLOYMGR_ADMIN_PASSWORD/g" credentials.txt


Expected ouput in credentials.txt :
installationHome=/opt/software
password=Mypm@123


Actual output ( which is wrong):
installationHome=/opt/software
password=MypmDEPLOYMGR_ADMIN_PASSWORD123
# 4  
Old 11-20-2011
Your password was "Mypm&123", you need to escape the "&" with
Code:
password="Mypm\&123"

# 5  
Old 11-21-2011
That i cant enforce while getting the input. Script should be intelligent enough to put in escape characters and thats what I am looking for.
# 6  
Old 11-21-2011
Well, what's your shell?
# 7  
Old 11-21-2011
No sure what OS, shell, or version of sed you're using, but this may help point you in the right direction. You can insert another line to have sed search for any non-alphanumeric characters, in this case a "&" and escape it, the proceed with the replacement in the file:

Code:
echo $DEPLOYMGR_ADMIN_PASSWORD
Mypm&123

DEPLOYMGR_ADMIN_PASSWORD=$(echo $DEPLOYMGR_ADMIN_PASSWORD | sed 's#\([]\#\%\@\*\$\/&[]\)#\\\1#g')

echo $DEPLOYMGR_ADMIN_PASSWORD
Mypm\&123

sed -i "s/DEPLOYMGR_ADMIN_PASSWORD/$DEPLOYMGR_ADMIN_PASSWORD/g" credentials.txt

installationHome=INSTALLATION_HOME
password=Mypm&123

Hope this helps.
This User Gave Thanks to in2nix4life 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

awk conditions failing (special characters?)

This is really frustrating because I can't figure it out. I'm running a health check script. One of the items I'm checking is the amount of memory on a server. I use the free command, which outputs something like this (excerpt) Mem: 100 100 100 100 Swap: 100 100 100 100 In my debugging... (5 Replies)
Discussion started by: JustaDude
5 Replies

2. 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

3. Shell Programming and Scripting

Escape special characters in SED

Need help in escaping special characters in sed command. Here is the the string which i am trying to find a replace with From :- REQUEST_TYPE=PIXEL&MSG_ID={//MESSAGE_ID} To :- REQUEST_TYPE=PIXEL&MSG_ID= X_EDELIVERY_MESSAGE_ID & BATCH_ID= X_EDELIVERY_BATCH_ID Here is the sed command i am... (2 Replies)
Discussion started by: aakishore
2 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 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

7. 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

8. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: linuxnewbeee
3 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