I am attempting to port javascript code to Java. In so doing, I need to replace all of the single quoted strings with double-quoted strings. This also requires me to replace the double-quotes with escaped double-quotes. But I only want to escape the quotes within single-quote string blocks.
I can replace the quoted strings with no problem, using the following sed command:
sed "s/'\([^']*\)'/\"\1\"/g"
This successfully modifies the single-quoted strings to double-quoted strings. But I still have to escape the internal double-quotes. The easiest way would seem to be if sed provided a method by which to run a regex replacement on a section of a line. But I do not know if that is possible.
I don’t think you can do it with
sedbecause its POSIX regex engine doesn’t know lookaround. But it would be possible in (for example) a Python script, by splitting up the operation into two steps: