user1835630
user1835630

Reputation: 261

single quote inside double quotes in shell script

I would need to replace in a file strings like "'a" with strings like 'a. In practice, I need to remove the double quotes.

I was thinking to use sed to do it, but I could not find a solution til now: I guess I am making some syntax errors because of the quotes.

Upvotes: 2

Views: 1546

Answers (4)

Vijay
Vijay

Reputation: 67319

you could use :

perl -pe 's/\042//g' your_file

042 is octal value of double quotes.

tested below:

> cat temp
"'a"
> cat temp | perl -pe 's/\042//g'
'a
> 

Upvotes: 0

Lee Netherton
Lee Netherton

Reputation: 22562

If you just need to remove all double quote characters from the file then you can use tr with the -d option:

$ cat test.txt
this is a test "'a"
something "else"
doesn't touch single 'quotes'

$ cat test.txt | tr -d '"'
this is a test 'a
something else
doesn't touch single 'quotes'

Update:

If you want to replace the specific instance of "'a" with 'a then you can use sed:

sed "s|\"'a\"|'a|g" test.txt
this is a test 'a
something "else"
doesn't touch single 'quotes'

However, I suspect that you are after something more general than just replacing quote markes around an a character. This sed command will replace any instance of "'anything" with 'anyhting:

sed "s|\"'\([^\"]\+\)\"|'\\1|g" test.txt
this is a test 'a
something "else"
doesn't touch single 'quotes'

Upvotes: 2

potong
potong

Reputation: 58578

This might work for you (GNU sed):

sed 's/"\('\''[^"]*\)"/\1/g' file

Upvotes: 0

miono
miono

Reputation: 344

This seems to work for me

echo '"a"' | sed "s/\"a\"/\'a/"

Upvotes: 0

Related Questions