PDA

View Full Version : simple bash idiocy



koszta
December 12th, 2010, 07:59 PM
Hi I am totally missing something...
I have a file that has double quotes in it and I want to escape them first (so that I can read lines in bash afterwards... how do I do that?

I tried :

sed "s/\"/\\\"/g" file.php

and it did nothing

I tried tr '\"' '\\\"' < file.php

and nothing :(
I know it is stupid ...what am i missing ?

thnx

Arndt
December 12th, 2010, 08:14 PM
Hi I am totally missing something...
I have a file that has double quotes in it and I want to escape them first (so that I can read lines in bash afterwards... how do I do that?

I tried :

sed "s/\"/\\\"/g" file.php

and it did nothing

I tried tr '\"' '\\\"' < file.php

and nothing :(
I know it is stupid ...what am i missing ?

thnx

I think you're doing something unnecessarily roundabout if you first escape the " in the file (and what happens to an already escaped "?), but I don't know what you will do with the file anyway.

This works:


sed "s/\"/\\\\\"/g"

and this:


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

I don't think 'tr' can replace one character with several.

trent.josephsen
December 12th, 2010, 09:59 PM
I have a file that has double quotes in it and I want to escape them first (so that I can read lines in bash afterwards...

I wish you would clarify this, because it sounds like you're trying to do something completely unnecessary.

saulgoode
December 12th, 2010, 10:02 PM
I tried :

sed "s/\"/\\\"/g" file.php

and it did nothing
:
:
I know it is stupid ...what am i missing ?
Since you are using soft quotes around the entire substitution expression, BASH is stripping the escapes before they are passed to 'sed'.

Instead, try using hard quoting:


sed 's/\"/\\\"/g' file.php

Arndt
December 12th, 2010, 10:06 PM
Since you are using soft quotes around the entire substitution expression, BASH is stripping the escapes before they are passed to 'sed'.

Instead, try using hard quoting:


sed 's/\"/\\\"/g' file.php

Similar to what I wrote. In what way is it better?

saulgoode
December 12th, 2010, 10:38 PM
Similar to what I wrote. In what way is it better?
Sorry, I had just woken from a nap and hadn't read your post accurately. Your way is actually better because it removes some unnecessary escaping.