title: Find and replace a substring in shell script
body: <p>I have a file which contains some strings for example in file <code>my_file.txt</code>, I have strings </p>
  
  <pre><code>foo_eusa.r1
  foo_chnc.r5
  foo_deu.r10
  .
  .
  .
  </code></pre>
  
  <p>Now I wanted to check for a substring whether it exists in the file and if it exists I wanted to modify the entire string or if it does not I will add it.</p>
  
  <p>For example I have a new string <code>foo_eusa.r4</code> I wanted to search for all occurences of substring <code>foo_eusa</code> in the file. If it exists (in the above it exists <code>foo_eusa.r1</code>) then replace <strong>r1</strong> with <strong>r4</strong> so the string becomes <code>foo_eusa.r4</code> instead of <code>foo_eusa.r1</code> in all occurences. If <code>foo_eusa</code> does not exist then the new string <code>foo_eusa.r4</code> is to be added</p>
  
  <p>I treid checking using grep -q but it gives only the first match and also could not find a way to replace the sub strings</p>