title: SED or AWK script to replace multiple text
body: <p>I am trying to do the following with a sed script but it's taking too much time. Looks like something I'm doing wrongly.</p>
  
  <p>Scenario:
  I've student records (> 1 million) in <code>students.txt</code>.
  In This file (each line) 1st 10 characters are student ID and next 10 characters are contact number and so on</p>
  
  <p><strong>students.txt</strong></p>
  
  <blockquote>
    <p>1000000001<strong>9234567890</strong>XXX... <br>
    1000000002<strong>9325788532</strong>YYY... <br>
    .<br>
    .<br>
    .<br>
    1001000000<strong>8766443367</strong>ZZZZ...</p>
  </blockquote>
  
  <p>I have another file (encrypted_contact_numbers.txt) which has all the phone but numbers and corresponding encrypted phone numbers as below</p>
  
  <p><strong>encrypted_contact_numbers.txt</strong></p>
  
  <blockquote>
    <p><strong>Phone_Number, Encrypted_Phone_Number</strong> <br><br>
    9234567890, 1122334455 <br>
    9325788532, 4466742178 <br>
    .<br>
    . <br>
    .<br>
    8766443367, 2964267747 <br></p>
  </blockquote>
  
  <p>I wanted to replace all the contact numbers (11th&ndash;20th position) in <code>students.txt</code> with the corresponding encrypted phone number from <code>encrypted_contact_numbers.txt</code>.</p>
  
  <p><strong>Expected Output:</strong></p>
  
  <blockquote>
    <p>1000000001<strong>1122334455</strong>XXX... <br>
    1000000002<strong>4466742178</strong>YYY... <br>
    . <br>
    . <br>
    . <br>
    1001000000<strong>2964267747</strong>ZZZZ...</p>
  </blockquote>
  
  <p>I am using the below sed script to do this operation. It is working fine but too slowly.</p>
  
  <p><strong>Approach 1:</strong></p>
  
  <pre><code>while read -r pattern replacement; do   
      sed -i "s/$pattern/$replacement/" students.txt
  done &lt; encrypted_contact_numbers.txt
  </code></pre>
  
  <p><strong>Approach 2:</strong></p>
  
  <pre><code>sed 's| *\([^ ]*\) *\([^ ]*\).*|s/\1/\2/g|' &lt;encrypted_contact_numbers.txt |
  sed -f- students.txt &gt; outfile.txt
  </code></pre>
  
  <p>Is there any way to process this huge file quickly?</p>
  
  <p><strong>Update: 9-Feb-2018</strong> <br> <br>
  Solutions given in AWK and Perl is working fine if the phone number is in specified position (column 10-20), If I try to do global replacement it took too much time to process. Is there any best way to achieve this?</p>
  
  <p><strong>students.txt : Updated version</strong></p>
  
  <blockquote>
    <p>1000000001<strong>9234567890</strong>XXX...<strong>9234567890</strong> <br>
    1000000002<strong>9325788532</strong>YYY... <br>
    .<br>
    .<br>
    .<br>
    1001000000<strong>8766443367</strong>ZZZZ<strong>9234567890</strong>...</p>
  </blockquote>