Learner
Learner

Reputation: 483

How to replace single quotes to double quotes in bash?

i have a following string:

 {"name":"ram","details":[{'subjects':{'service_location':'c:/A.exe','url':'http://A.zip'}}]}

In the above string few strings have single quotes around them. so, how can i replace single quotes with double quotes in the above string and get as follows:

 {"name":"ram","details":[{"subjects":{"service_location":"c:/A.exe","url":"http://A.zip"}}]}

Upvotes: 3

Views: 10296

Answers (4)

Mark Setchell
Mark Setchell

Reputation: 207375

With tr to translate individual characters:

your_command | tr "'" '"'

Or, if that is too much typing:

your_command | tr \' \"

:-)

Upvotes: 1

MicroPyramid
MicroPyramid

Reputation: 1630

Extending @RavinderSingh13 answer, use following command to convert your string.

echo '{"name":"ram","details":'"[{'subjects':{'service_location':'c:/A.exe','url':'http://A.zip'}}]}" | sed "s/'/\"/g"

Upvotes: 3

RavinderSingh13
RavinderSingh13

Reputation: 133428

@Learner: Try a sed solution:

sed "s/'/\"/g"  Input_file

OR

your_command | sed "s/'/\"/g" 

Upvotes: 8

P....
P....

Reputation: 18351

Following will convert all the single quotes to double quotes. This used awk's inbuilt gsub function to search and replace the text. Here, we are telling awk to replace s which is equal to single quotes. Once single quote is found replace it with double quote. 1 is to print the line.

awk -v s="'" '{gsub(s,"\"")}1' file

OR

source-of-json | awk -v s="'" '{gsub(s,"\"")}1'

Upvotes: 0

Related Questions