title: Creating two files from one file using unix
body: <p>I have one input file, I need to read and apply some condition and route to two other files.</p>
  
  <pre><code>100 COMPANY Records
  500ABC COMPANY 345         
  2pen9999out
  2cow7777out
  2goa7777out
  500ABC COMPANY IN 456
  2car9999out
  2cow7777out
  2HAT7777out
  2BAL9999out
  2BAL6666out
  </code></pre>
  
  <p>here, record start with 5 was header and 2 was detail</p>
  
  <p>i need to create two file <code>ABC_COMPANY.txt</code> and <code>ABC_COMPANY_IN.txt</code>?</p>
  
  <p>I have written below logic, which I want to convert using <code>awk</code> or other right approach?</p>
  
  <pre><code>NUMBER1="666"
  NUMBER2="777"
  while read line
   do
  
   REC_IND=`echo "${line}" | awk '{print substr($1,1,1)}'`
  
   if [ "${REC_IND}" = "5" ]; then
   FILENAME="ABC_COMPANY"
   DEAIL_COMPANY=`echo "${line}" | awk '{print substr($0,3,7)}'`
    if [[ $DEAIL_COMPANY = "ABC COMPANY IN" ]]; then
      FILENAME="ABC_COMPANY_IN"
    fi
  fi
  #check for detail record
    if [ "${REC_IND}" = "2" ] ;then
   #substring to find value
   Value=`echo "${line}" | awk '{print substr($0,4,9)}'`
    #if record belongs to bank then route to the respective file
    if [ "$Value" = "$NUMBER1" ]  || [ "$Value" = "$NUMBER2" ] ; then
      echo $line &gt;&gt; ${FILENAME}".txt"
    fi
  fi
  done &lt;  /bk/shan.txthere
  </code></pre>
  
  <p>expected output:</p>
  
  <p><strong>ABC_COMPANY.txt</strong></p>
  
  <pre><code>2cow7777out
  2goa7777out
  </code></pre>
  
  <p><strong>ABC_COMPANY_IN.txt</strong></p>
  
  <pre><code>2cow7777out
  2HAT7777out
  2BAL6666out
  </code></pre>