title: How to grep content of file and create another file with grepped content?
body: <p>I want to get grep content of file matching particular text and then want to save all those records which matches particular text to new file and also want to make sure that matched content is removed from original file. </p>
  
  <pre><code>    296949657|QL|163744584|163744581|20441||
      292465754|RE|W757|3012|301316469|00|
      296950717|RC|7264|00001|013|27082856203|
      292465754|QL|191427266|191427266|16405||
      296950717|RC|7264|AETNAACTIVE|HHRPPO|27082856203|
      299850356|RC|7700|153447|0891185100102-A|W19007007201|
      292465754|RE|W757|3029|301316469|00|
      299850356|RC|7700|153447|0891185100104-A|W19007007201|
      293695591|QL|743559415|743559410|18452||
      297348183|RC|6602|E924|0048|CD101699303|
      297348183|RC|6602|E924|0051|CD101699303|
      108327882|QL|613440276|613440275|17435||
  </code></pre>
  
  <p>I have written awk and it works as expected for small files but for larger files is not working as expected....am sure that i have missed something...</p>
  
  <pre><code>awk '{print $0 &gt; ($0~/RC/?"RC_RECORDS":"TEST.DAT")}' TEST.DAT
  </code></pre>
  
  <p>any thoughts on how to fix this. </p>
  
  <p><strong>Update 1</strong></p>
  
  <p>Now in above file, i always want to check values of column two to |RC| and if it matches then move that record to RC_RECORDS file and if values matches to |RE| then move it to RE_RECORDS, how can this be done. </p>
  
  <p>Case 1: </p>
  
  <p>So for example if i have records as </p>
  
  <pre><code>108327882|RE|613440276|613440275|RC||
  </code></pre>
  
  <p>then it should go to RE_RECORDS file. </p>
  
  <p>Case 2: </p>
  
  <pre><code>108327882|RC|613440276|613440275|RE||
  </code></pre>
  
  <p>then it should go to RE_RECORDS</p>
  
  <p>Case 3: </p>
  
  <pre><code>108327882|QL|613440276|613440275|RC||
  </code></pre>
  
  <p>then it should not go to either RE_RECORDS or RC_RECORDS </p>
  
  <p>Case 4:</p>
  
  <pre><code>108327882|QL|613440276|613440275|RE||
  </code></pre>
  
  <p>then it should not go to either RE_RECORDS or RC_RECORDS </p>
  
  <p>My hunch is that </p>
  
  <pre><code>awk '/\|RC\|/ {print &gt; "RC_RECORDS.DAT";next} {print &gt; "NEWTEST.DAT"}' TEST.DAT | awk '$2 == "RC"'
  awk '/\|RE\|/ {print &gt; "RE_RECORDS.DAT";next} {print &gt; "FINAL_NEWTEST.DAT"}' NEWTEST.DAT | awk '$2 == "RE"'
  </code></pre>
  
  <p>but wanted to check if there's an better and quicker solution out there that can be used.</p>
  
  <p><strong>Update 2</strong> </p>
  
  <p><img src="https://i.stack.imgur.com/jd4Xj.jpg" alt="enter image description here"></p>
  
  <p><strong>Update 3</strong>
  <img src="https://i.stack.imgur.com/ztxyP.jpg" alt="enter image description here"></p>