How to substitute variable in sed for special character?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to substitute variable in sed for special character?
# 1  
Old 04-28-2012
How to substitute variable in sed for special character?

Hi ,

I have input file like below

Code:
Hi this is "vinoth".
Hi happy to work with 'unix'
USA(united states of America)

My script variables are below :

Code:
Dquote=Ộ
Squote=&#$567
Obrac=&^986
Cbrac=&^745

I want to read the variables in my SED command to replace the double quote,single quote,open bracket, and close bracket.

I want output file like

Code:
Hi this is ỘvinothỘ.
Hi happy to work with &#$567unix&#$567
USA&^986united states of America&^745.

Please help me .Thanks in advance.

Last edited by Scrutinizer; 04-28-2012 at 09:57 AM.. Reason: code tags
# 2  
Old 04-28-2012
Hi, try:
Code:
sed "s/\"/$Dquote/g; s/\'/$Squote/g; s/(/$Obrac/g; s/)/$Cbrac/g" infile

# 3  
Old 04-29-2012
Hi Scrutinizer ,

Code:
sed "s/\"/$Dquote/g; s/\'/$Squote/g; s/(/$Obrac/g; s/)/$Cbrac/g" infile

It is not working ,Iam getting below output

Code:
script.bash: line 4: ^986: command not found
script.bash: line 5: ^745: command not found
1.hi this is ỘvinothỘ.
2.iam software ỘengineerỘ.
3.How is life not good.

Please help

Moderator's Comments:
Mod Comment Please click this link: How to use [code][/code] tags

Last edited by Scrutinizer; 04-29-2012 at 08:16 AM.. Reason: code tags
# 4  
Old 04-29-2012
Hello, could you show us what is on lines 4 and 5 of script.bash ?

--
please use code tags
# 5  
Old 04-29-2012
Hi scrutinizer,

i tried the below script,

Code:
#!/bin/bash

Dquote=ͺ
Squote=&#$567
Obrac=&^986
Cbrac=&^745

Code:
sed "s/\"/$Dquote/g; s/\'/$Squote/g; s/(/$Obrac/g; s/)/$Cbrac/g" file


Below is my input file

Code:
1.hi this is "vinoth".
2.iam software 'engineer'.
3.How is life (not good).
4.i have used version 1.0 as the software.And yu are the first price if 1.06.wat is involved?

Moderator's Comments:
Mod Comment Please click this link: How to use [code][/code] tags

Last edited by Scrutinizer; 04-29-2012 at 07:01 AM.. Reason: code tags
# 6  
Old 04-29-2012
Hi, try with single quotes to protect special characters
Code:
Dquote=ͺ
Squote='&#$567'
Obrac='&^986'
Cbrac='&^745'


Last edited by Scrutinizer; 04-29-2012 at 07:05 AM..
# 7  
Old 04-29-2012
yes if protected with single quotes error is getting ignored.

But still output is coming like this..

Code:
1.hi this is "#890vinoth"#890.#$567
2.iam software 'engineer'.#$567
3.How is life (^986not good)^745.#$567
4.i have used version 1.0 as the software.And yu are the first price if 1.06.wat is involved?#$567
#$567

I should not get that single or double quotes or brackets instead it should be replaced with the given values which is substituted through vatiables.

Please help.

Last edited by Scrutinizer; 04-29-2012 at 07:17 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Substitute a character with sed

hi all, i'd like to modify a file with sed , i want to substuite a char "-" with "/" how can i do this? Thanks for all regards Francesco (16 Replies)
Discussion started by: Francesco_IT
16 Replies

2. Shell Programming and Scripting

How to handle variable with special character?

Hi Gurus, I have a requirement which needs to pass a parameter when calling the script, using this parameter to find a file name stored in master file. then read the file content. the issue is in the file name has a special character "$". don't know how to handle this. below is example: the... (10 Replies)
Discussion started by: green_k
10 Replies

3. Shell Programming and Scripting

sed add special character

Hi all I got test.test.test and need test.test\.test * I need the backslash before the last dot in the line I tried echo test.test.test | sed 's/\./\\./g' but it gives me test\.test\.test Thanks (7 Replies)
Discussion started by: stinkefisch
7 Replies

4. UNIX for Dummies Questions & Answers

Replacing special character with sed

Hi All, I have a text file that contains I1SP2 *=*=Y=M=D001D My requirement is to replace all occurrence of =* to =Z expected o/p is I1SP2 *=Z=Y=M=D001D I have tried with sed 's/=*/=Z/g' file sed 's!\=*!\=Z/g' file sed 's!\=*!\=Z!g' file sed 's!\=\*!\=Z!g' file but its not... (3 Replies)
Discussion started by: gotamp
3 Replies

5. Shell Programming and Scripting

How to replace special character using sed?

How can I replace the follong text including to number 7000? cat tmp0.txt Winston (UK) Wong I would the 7000 to replace Winston (UK) Wong. I fail with method below: sed ' s /Winston\(UK\)Wong/7000 tmp0.txt' (1 Reply)
Discussion started by: vivien_chu
1 Replies

6. Shell Programming and Scripting

how to replace the special character with another using SED

I have the replace the pattern in the file , ); to ); Could someone please help me to get this command. (2 Replies)
Discussion started by: mohan.bit
2 Replies

7. Shell Programming and Scripting

sed special character replace

I need to do the following: text in the format of: ADDRESS=abcd123:1111 - abcd123:1111 is different on every system. replace with: ADDRESS=localhost:2222 sed 's/ADDRESS=<What do I use here?>/ADDRESS=localhost:2222/g' Everything I've tried ends up with: ... (3 Replies)
Discussion started by: toor13
3 Replies

8. Shell Programming and Scripting

Decode %s Special Character in Sed

Greetings, I am doing something that I don't know if it is possible... I have a file with a line looks like this: <%s \n%s / %s \n%s \n> and I am trying to replace this line with <%s \n%s \n%s / %s \n%s \n> in Shell script with sed command... StringToReplace='%s \n%s / %s \n%s \n'... (2 Replies)
Discussion started by: wasabihowdi
2 Replies

9. UNIX for Dummies Questions & Answers

read a variable character by character, substitute characters with something else

im having trouble doing this: i have a variable with 2 characters repeating e.g. aababbbaaaababaabbaabbba is there a way i can search the variable for a's and b's and then change a's to b's and b's to a's? im guessing its like getting the 1's compliment of the string im doing this in... (2 Replies)
Discussion started by: vipervenom25
2 Replies

10. Shell Programming and Scripting

Sed-Special character replacement

Hi I want to replace ./testsed.ksh with testsed.ksh ./ is to be removed scriptnm=`sed -e 's/\.///' $0 does not work Please help (3 Replies)
Discussion started by: usshell
3 Replies
Login or Register to Ask a Question