title: Bash Scripting: Replace (or delete) string in a file if line starts with (or matches) another string
body: <p>Assuming an ini-style file like this,</p>
  
  <pre><code>[Group]
  Icon=xxx.ico
  Title=An Image Editor
  Description=Manipulates .ico, .png and .jpeg images
  </code></pre>
  
  <p>I want to replace/delete ".ico" ONLY in the line that starts with (or matches) "Icon="</p>
  
  <p>I was trying this:</p>
  
  <pre><code>oldline="`cat "$file" | grep "Icon="`"
  newline="`echo "$oldline" | tr ".ico" ".png"`"
  cat "$oldfile" | tr "$oldline" "$newline" &gt; $file
  </code></pre>
  
  <p>Then i realized that <code>tr</code> works completely different than i thought. Its NOT a tradicional "replace this for that" function. So i guess the correct way is using <code>sed</code>. But:</p>
  
  <ul>
  <li>Ive never used <code>sed</code>before. No idea how it works. Is it overkill?</li>
  <li>If the most indicated way is really using <code>sed</code>, given it is so powerful, is there any elegant way to accomplish this rather than this "fetch line -> modify line -> replace oldline for newline in file" approach?</li>
  </ul>
  
  <p>Notes:</p>
  
  <ul>
  <li>I cant replace ".ico" globally, i know that would be a LOT easier, i must restrict the replace to the Icon line, otherwise the Description line would be changed too.</li>
  <li>Im new to shell scripting in Linux, so im looking not only to the solution itself, but also for the "proper" way to do it.  Elegant, easy to read, conventional, etc</li>
  </ul>
  
  <p>Thanks in advance!</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Thank you guys! Here is the final script, as a reference:</p>
  
  <pre><code>#! /bin/bash
  # Fix the following WARNING in ~/.xsession-errors
  # gnome-session[2035]: EggSMClient-WARNING: Desktop file '/home/xxx/.config/autostart/skype.desktop' has malformed Icon key 'skype.png'(should not include extension)
  
  file="$HOME/.config/autostart/skype.desktop"
  
  if [ -f "$file" ] ; then
      if `cat "$file" | grep "Icon=" | grep -q ".png"` ; then
          sed -i.bak '/^Icon=/s/\.png$//' "$file"
          cp "$file" "$PWD"
          cp "${file}.bak" "$PWD"     
      else
          echo "Nothing to fix! (maybe fixed already?)"
      fi  
  else
      echo "Skype not installed (yet...)"
  fi
  </code></pre>
  
  <p>MUCH sleeker than my original! The only thing i regret is that <code>sed</code> backup does not preserve original file timestamp. But i can live with that. </p>
  
  <p>And, for the record, yes, ive created this script to fix an actual "bug" in Skype packaging. </p>