Need help with sed to escape special characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with sed to escape special characters
# 1  
Old 02-03-2012
Question Need help with sed to escape special characters

Hello Everyone,

I need to read an encrypted password from the user and update that value in an xml file. I am trying to use "sed" for searching the appropriate tag and replacing this new value that get from the user. Since the encrypted password can contain special characters(like /,\,&,etc), the "sed" command is coming garbled. Could somebody please help to find an efficient solution for this problem?

Is there a way to instruct sed command not to consider any special characters in the replacement string?

Something like
Code:
s/${key}/"what ever string"/

Or

Any efficient way to put escape sequence for the special characters?

I tried something like
Code:
's/\|/\\|/g' , 's|\\|\\\\|g'

to escape individual special characters. But this is not working for special characters like &,/ etc.... Is it possible to write any generic pattern which can handle any special characters.

Very much appreciate any help

Thanks,
Martin

Last edited by Franklin52; 02-03-2012 at 02:09 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 02-03-2012
Delimiter in sed can be anything, not just slash:
Code:
sed 's!what!that!'

# 3  
Old 02-06-2012
Hello Mirni,

Thanks for the response. I have problem here as I do not know the search string in advance, it's being read from the user. So the user can enter any character in the input, which may contain the same delimiter character that I used..

Thanks,
Martin
# 4  
Old 02-06-2012
use : as delimiter. Password will not contain the the :
sed 's:searchstring:replacestring:g'

Thanks,
Kalai
# 5  
Old 02-06-2012
What exactly are you trying to do with the sed command?
If you are storing the passwords, you should consider hashing them and store the hashes instead (in which case you cannot retrieve them, you'll need to reset them).
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 escape all special characters?

I have an application which I am integrating with that accepts the password via a CLI. I am running in to issues with passwords that contain special characters. I tried to escape them all, but I ran in to an issue where I cannot escape the characters ' ] My attempt is as follows: $... (2 Replies)
Discussion started by: AMG1978
2 Replies

2. Shell Programming and Scripting

How to escape Special Characters in Expect programming?

Hi, I have written a unix expect utility "ssh-login.exp" which connects (ssh) to remote host and execute some shell script. I am calling this "ssh-login.exp" utility from another shell script. "ssh-login.exp" takes username, password, host and shell script path to execute on remote host. All... (1 Reply)
Discussion started by: Mahesh Desai
1 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

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

5. Shell Programming and Scripting

Replace new line with <br /> & escape special characters

Hi, I wish to replace a new line with br (html) but it doesn't seem to work message=$(echo ${FORM_message} | tr '\r' '<br \/>' ) what it gives me seems to be ... b...? I am also having problem escaping hash sign in cut command: list=$(echo "$line" | cut -d'\#;\#' -f1) ; my intention is... (2 Replies)
Discussion started by: ted_chou12
2 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

awk print $1 escape all special characters

I'm using awk '{print $1}' and it works most of the time to print the contents of a mysql query loop, but occationally I get a field with some special character in it, is there a way to tell awk to ignore all special characters between my FS? I have >186K records, so building a list of ALL special... (6 Replies)
Discussion started by: unclecameron
6 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. UNIX for Dummies Questions & Answers

Need help to escape special characters in Korn shell script

Hi, I would like to display the following message from my shell (Korn) script Copy "old_file.txt" to "new_file.txt" My code looks as follows print "Copy "old_file.txt" to "new_file.txt"" However, when I execute the script, I get the following output Copy old_file.txt to... (6 Replies)
Discussion started by: rogers42
6 Replies

10. 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
Login or Register to Ask a Question