How to substitute variable in sed for special character?

Hi ,

I have input file like below

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

My script variables are below :

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

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

Please help me .Thanks in advance.

Hi, try:

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

Hi Scrutinizer ,

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

It is not working ,Iam getting below output

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

Hello, could you show us what is on lines 4 and 5 of script.bash ?

--
please use code tags

Hi scrutinizer,

i tried the below script,

#!/bin/bash

Dquote=
Squote=&#$567
Obrac=&^986
Cbrac=&^745
sed "s/\"/$Dquote/g; s/\'/$Squote/g; s/(/$Obrac/g; s/)/$Cbrac/g" file

Below is my input file

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?

Hi, try with single quotes to protect special characters

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

yes if protected with single quotes error is getting ignored.

But still output is coming like this..

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.

Ow, and escape the special meaning in the replacement part of the substitution command in sed..:

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

output:

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

Hi,

Below is input

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?

I want below output,

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?

If i find single quotes it should be replaced by ,If double quotes are found any where in the file its should be replaced by .It should not print the ',"",(,and, ).

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

output:

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?

Or simply use:

sed "s/\"//g; s/\'//g; s/(//g; s/)//g" infile

or:

sed "y/\"'()//" infile