Replacing string with special characters in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing string with special characters in shell
# 1  
Old 02-23-2011
Replacing string with special characters in shell

Hi,

I am trying to replace a string in shell but it is not working correctly.

Code:
@xcom.file@

needs to be replaced with
Code:
tb137

Plz help.Thx.

Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Last edited by zaxxon; 02-23-2011 at 11:22 AM.. Reason: code tags
# 2  
Old 02-23-2011
Code:
sed 's/@xcom.file@/tb137/g' infile

If you use bash, you could also do something like this:
Code:
$> VAR="1234@xcom.file@5678@xcom.file@90"
$> echo ${VAR//@xcom.file@/tb137}
1234tb1375678tb13790

# 3  
Old 02-24-2011
modification needed

Thanks for the response Smilie
Need more modifications though.
file1:
Code:
RECLASS:@xcom.reclass.hostname@:REMOTE_SYSTEM=@xcom.reclass.remote_system@

This needs to be replaced with value of variables declared in file2:
Code:
input.xcom.reclass.hostname=tb1234
input.xcom.reclass.remote_system=tbj1234545

desired op:file1 should be:
Code:
RECLASS:tb1234:REMOTE_SYSTEM=tbj1234545

# 4  
Old 02-24-2011
Code:
awk 'NR==FNR{gsub(/input./,""); split($0,a,"=");b[a[1]]=a[2];next} {gsub(/@/,"");for (i in b) gsub(i,b[i])}1' file2 file1

# 5  
Old 03-04-2011
Appreciate your help!It works..Just that the output of awk operation is coming in a single line.From the man page of awk,I realized that gsub,split don't recognize new line characters. Is there any way that the op can be in this format?
File 1:
Code:
input.xcom.range.hostname = DUMMY
input.xcom.range.remote_system = DUMMY
input.xcom.range.userid = DUMMY
input.xcom.range.password = DUMMY
input.xcom.range.remote_file_system = DUMMY
input.xcom.reclass.hostname = tb137
input.xcom.reclass.remote_system = DUMMY
input.xcom.reclass.userid = DUMMY
input.xcom.reclass.password = DUMMY
input.xcom.reclass.remote_file_system = DUMMY

File 2:
Code:
RANGE:@xcom.range.hostname@:REMOTE_SYSTEM=@xcom.range.remote_system@  USERID=@xcom.range.password@ PASSWORD=@xcom.reclass.password@:REMOTE_FILE=@xcom.range.remote_file_system @:
RECLASS:@xcom.reclass.hostname@:REMOTE_SYSTEM=@xcom.reclass.remote_system@ USERID=@xcom.reclass.userid@ PASSWORD=@xcom.reclass.password@:REMOTE_FILE==@xcom.reclass.remote_file_system@:

Desired op is:file 2 should be:
Code:
RANGE:DUMMY:REMOTE_SYSTEM=DUMMY USERID=DUMMY PASSWORD=DUMMY:REMOTE_FILE=DUMMY:
RECLASS:tb137:REMOTE_SYSTEM=DUMMY USERID=DUMMY PASSWORD=DUMMY:REMOTE_FILE=DUMMY:

After running the command now,it comes in a single line.
Code:
RANGE:DUMMY:REMOTE_SYSTEM=DUMMY USERID=DUMMY PASSWORD=DUMMY:REMOTE_FILE=DUMY:RECLASS:tb137:REMOTE_SYSTEM=DUMMY USERID=DUMMY PASSWORD=DUMMY:REMOTE_FILE=DUMMY:


Last edited by manish72; 03-04-2011 at 05:52 AM.. Reason: corrcted format of desired op.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Any tip to replacing the special characters in a file

Hi, Please find attached a file that has special characters on it. It is a copy and paste from a Micro$oft file. I don't want to use strings as it remove all the 'indentations' / 'formatting' so I am replacing them with space instead. I am using the sed command below sed "s/$(printf... (1 Reply)
Discussion started by: newbie_01
1 Replies

2. UNIX for Beginners Questions & Answers

Replacing string/special characters using a 'conversion' table

Hi, Does anyone know if there is a script or program available out there that uses a conversion table to replace special characters from a file? I am trying to remove some special characters from a file but there are several unprintable/control characters that some I need to remove but some I... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. UNIX for Dummies Questions & Answers

Replacing valuses containig space and special characters

**Extremely sorry for the typos in heading Old:CAST ('${DEFAULT_HIGH_DATE}' AS DATE FORMAT 'YYYY-MM-DD') New :CAST(CAST('${G_DEFAULT_HIGH_DATE}' AS DATE FORMAT 'MM-DD-YYYY') as DATE FORMAT 'YYYY-MM-DD') Need to change old format as new format cat file1 CAST ('${DEFAULT_HIGH_DATE}' AS... (1 Reply)
Discussion started by: 100bees
1 Replies

4. Shell Programming and Scripting

Need help in replacing special characters

I am writing a ksh script. I need to replace a set of characters in an xml file. FROM="ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÛÚÜÝßàáâãäåçèéêëìíîïðñòóôõö¿¶ø®"; TO="AAAAAAACEEEEIIIIDNOOOOOOUUUUYSaaaaaaceeeeiiiionooooo N R" I have used the code- sed 's/$FROM/$TO/g'<abc.xml But its not working. Can anyone tell me the code to do this? (3 Replies)
Discussion started by: saga20
3 Replies

5. Shell Programming and Scripting

Remove string between two special characters

Hi All, I have a variable like AVAIL="\ BACK:bkpstg:testdb3.iad.expertcity.com:backtest|\ #AUTH:authstg:testdb3.iad.expertcity.com:authiapd|\ TEST:authstg:testdb3.iad.expertcity.com:authiapd|\ " What I want to do here is that If a find # before any entry, remove the entire string... (5 Replies)
Discussion started by: engineermayur
5 Replies

6. Programming

C++ Special Characters in a String?

Hello. How can i put all of the special characters on my keyboard into a string in c++ ? I tried this but it doesn't work. string characters("~`!@#$%^&*()_-+=|\}]{ How can i accomplish this? Thanks in advance. (1 Reply)
Discussion started by: cbreiny
1 Replies

7. Shell Programming and Scripting

help on sed replacing special characters

Hello, I have a file with many lines with below format: \abc\\1234 jkl\\567 def\\345 \pqr\\567 \xyz\\234 Here, i need to do 2 things. 1. replace \\ with \ 2. remove starting \ so output to be as below: (11 Replies)
Discussion started by: prvnrk
11 Replies

8. Shell Programming and Scripting

Replacing French special characters

Hi, I have tonnes of .txt files that are written in French. I need to replace the French special characters, however, with English equivalents (e.g. é -> e and ç -> c). I have tried this --- #!/bin/bash # Convert French characters to normal characters # Treat each of the files exec... (4 Replies)
Discussion started by: BlueberryPickle
4 Replies

9. Shell Programming and Scripting

Add string after another string with special characters

Hello everyone, I'm writing a script to add a string to an XML file, right after a specified string that only occurs once in the file. For testing purposes I created a file 'testfile' that looks like this: 1 2 3 4 5 6 6 7 8 9 And this is the script as far as I've managed: ... (2 Replies)
Discussion started by: heliode
2 Replies

10. Shell Programming and Scripting

replacing string with special character ???

the problem is while replacing the old string with new one with the help of SED i am unable to replace the special characters with new strings. how can i do that? i dont want the user to be given the trouble to write '\' before every special characters like * , . , \ , $ , &. sed... (4 Replies)
Discussion started by: imppayel
4 Replies
Login or Register to Ask a Question