Awk Print Fixed Position Text File

By Robbe Morris

Small snippet to read a fixed position text file and report on which characters are in specific positions within the file. Great for troubleshooting text file exports.

BEGIN {OFS=""}
{

      rec_count += 1
  if (rec_count == 1)
     {
    print "     >.........1.........2.........3.........4.........5.........6.........7.........8.........9.........0"
    print "     >1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
    print " "
    }


  if (rec_count <= ENVIRON["rec_cnt"])
    {

      countme=0;
      start=1;
      tmp_cnt=0;

      while (countme < ENVIRON["prtme_loop"])
      {
       countme += 1
       lengthme=substr($0,start,100)
       len=length(lengthme)
       if (len > 0)
        {
           if (countme == 1)
             {
               print "0001 >",substr($0,start,100),"<- ",FNR
             }
               else
             {
               tmp_cnt += 1
              if (tmp_cnt < 10 )
               {
                  print "0",tmp_cnt,"00 >",substr($0,start,100),"<"
               }
              if (tmp_cnt > 9  )
               {
                  print tmp_cnt,"00 >",substr($0,start,100),"<"
               }
             }
           start += 100
        }
      }
       print " "
       print " "
       print " "

    }
}

Awk Print Fixed Position Text File  (1423 Views)