What is the best way to quote unquoted strings while leaving already quoted strings alone?
<p>I have a large block of text that is almost JSON, but is not quite. I need to make it JSON so that I can process it. Specifically, it is the "marks" object in the code at the following page: <a href="https://en.wikipedia.org/wiki/Module:Syrian_Civil_War_detailed_map" rel="nofollow">https://en.wikipedia.org/wiki/Module:Syrian_Civil_War_detailed_map</a></p>

<p>Some of the strings there are already quoted. Some of them are not. I need to quote all the strings and leave the already-quoted ones alone. For example, with the following almost-JSON-object:</p>

<pre><code>marks = {
    lat = 36.103,
    long = 37.308,
    mark = m.gov_hill,
    marksize = 184,
    label = "[[Battle of Aleppo (2012âpresent)|Aleppo]]",
    link = "Battle of Aleppo (2012âpresent)",
    label_size = 0,
    position = "left"
}
</code></pre>

<p>I need it to become this:</p>

<pre><code>"marks" = {
    "lat" = 36.103,
    "long" = 37.308,
    "mark" = "m.gov_hill",
    "marksize" = 184,
    "label" = "[[Battle of Aleppo (2012âpresent)|Aleppo]]",
    "link" = "Battle of Aleppo (2012âpresent)",
    "label_size" = 0,
    "position" = "left"
}
</code></pre>

<p>Essentially, anything that can become a string and is not already a string should be turned into a string. Also, I already have a sed command for turning the equals signs into colons as one of the steps to turning it into valid JSON, so no need to comment on that part of the process.</p>

<p>Thanks in advance!</p>

<p>This looks for any lines that does not already have any <code>"</code> characters.  For those lines have an alphabetic character or an underline after the equal sign, then the quantity after the equal sign is placed in quotes:</p>

<pre><code>$ sed -E '/"/!s/= +(.*[[:alpha:]_].*),/= "\1",/' file
marks = {
    lat = 36.103,
    long = 37.308,
    mark = "m.gov_hill",
    marksize = 184,
    label = "[[Battle of Aleppo (2012âpresent)|Aleppo]]",
    link = "Battle of Aleppo (2012âpresent)",
    label_size = 0,
    position = "left"
}
</code></pre>

<p>In more detail:</p>

<ul>
<li><p><code>-E</code></p>

<p>This tells sed to use extended regular expressions.</p></li>
<li><p><code>/"/!</code></p>

<p>This tells sed to remove from consideration any line that already has double quotes.</p></li>
<li><p><code>s/= +(.*[[:alpha:]_].*),/= "\1",/</code></p>

<p>This substitute command puts the quantity after the equal sign in double quotes if the quantity has either an alphabetic character or an underline character.</p></li>
</ul>

<h3>Changing the file in place</h3>

<p>To change a file in place, use the <code>-i</code> option:</p>

<pre><code>sed -i.bak -E '/"/!s/= +(.*[[:alpha:]_].*),/= "\1",/' file
</code></pre>

python json regex string sed