How to substitute variable in sed for special character?


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to substitute variable in sed for special character?
# 1  
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 10:57 AM.. Reason: code tags
# 2  
Hi, try:
Code:
sed "s/\"/$Dquote/g; s/\'/$Squote/g; s/(/$Obrac/g; s/)/$Cbrac/g" infile

# 3  
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 09:16 AM.. Reason: code tags
# 4  
Hello, could you show us what is on lines 4 and 5 of script.bash ?

--
please use code tags
# 5  
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 08:01 AM.. Reason: code tags
# 6  
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 08:05 AM..
# 7  
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 08:17 AM.. Reason: code tags
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #962
Difficulty: Medium
ILOVEYOU, sometimes referred to as Love Bug or Love Letter, was a computer worm that attacked tens of millions of Windows PCs on and after 5 May 2000 local time in the Philippines.
True or False?

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

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

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

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

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

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

10. Shell Programming and Scripting

sed and special character in data

I have a script that is reading an existing report, pulling out the customer code, then tacking on the customer name from another file and replacing the existing customer code with the new field. This was written for me by someone else. I'm not real familiar with sed. The data is getting into... (3 Replies)
Discussion started by: MizzGail
3 Replies

Featured Tech Videos