title: How do I grep for a word and then uncomment that line only if its commented
body: <p>Writing a bash script where I have to <code>grep</code> for <code>property1</code> in another file called <code>filename.conf</code> and check if that <code>property1</code>already exists. In a case where there is already <code>property1=correctvalue</code> in the filename.conf, I will first want to check whether there is commented or uncommented property1 using grep -q. If its present but its not equal to <code>correctvalue</code> then change it to <code>correctvalue</code> and uncomment if commented.</p>
  <p>Else, if the property1 doesn't exist, then write <code>property1=correctvalue</code>.</p>
  <p>Example:
  Sample input 1</p>
  <pre><code>property1 = correctvalue
  </code></pre>
  <p>Sample output 1  (where you do nothing)</p>
  <pre><code>property1 = correctvalue 
  </code></pre>
  <p>Sample input 2</p>
  <pre><code>property1=correctvalue
  </code></pre>
  <p>Sample output 2 (where you do nothing)</p>
  <pre><code>property1=correctvalue 
  </code></pre>
  <p>Sample input 3</p>
  <pre><code>property1= incorrectvalue
  </code></pre>
  <p>Sample output 3</p>
  <pre><code>property1=correctvalue
  </code></pre>
  <p>Sample input 4</p>
  <pre><code>#property1=correctvalue
  </code></pre>
  <p>Sample output 4 (uncommented it)</p>
  <pre><code>property1=correctvalue
  </code></pre>
  <p>Sample input 5</p>
  <pre><code>#property1=incorrectvalue 
  </code></pre>
  <p>Sample output 5  (uncomment it and change to correctedvalue)</p>
  <pre><code>property1=correctvalue
  </code></pre>
  <p>This is what I have so far:</p>
  <pre><code>if grep -q '^property1' filename.conf; then #How do I write it so it will grep for either &quot;#property1&quot; or &quot;property1&quot;?
      #if the content after &quot;=&quot; is not &quot;correctvalue&quot; ignoring the space before/after &quot;=&quot;, then correct it
      #if there is &quot;#&quot; in the beginning then remove the &quot;#&quot;
  else
      echo &quot;property1=correctvalue&quot; 
  fi
  </code></pre>