Using a variable to select records with awk

As part of a bigger task, I had to read thru a file and separate records into various batches based on a field. Specifically, separate records based on the value in the batch field as defined below. The batch field left-justified numbers.
The datafile is here

> cat infile
12345  1 John Smith   *
109    2 Joe Doe      *
1-9089 13Harry Can    *
14-12  5 Mary Will    *
43-4-5 13Kerry Lay    *
50505  1 Wanda Round  *

My script is here (and includes info on file layout)

> cat anal_bat
#! /usr/bin/bash
#
# analyze infile
#   acct_no 01-07
#   batch   08-09
#   name    10-22
#   extra   23

cnt=1
max=15
while [ $cnt -le $max ]
   do
   cnta=$(printf "%-2s" $cnt)
   echo $cnta

   awk 'substr($0,8,2)=="$cnta" {print}' <infile >outfile_${cnt}
   cnt=$((cnt+1))
done

This is all greatly simplified from my overall task which was then to perform data manipulations on the separated files. However, my small script above with the awk command merely creates 15 files that are empty.
So, what is wrong with the syntax?

joeyg,

You need to pass the input variable value (cnta) to awk and use cnta instead of $cnta in awk,
for eg:

 awk -v cnta=$cnta 'substr($0,8,2)==cnta {print}' <infile >outfile_${cnt}

Try this one, replace this line:

awk 'substr($0,8,2)=="$cnta" {print}' <infile >outfile_${cnt}

with:

awk -v var="$cnta" 'substr($0,8,2)==var {print}' <infile >outfile_${cnt}

Regards

Another solution to this..

awk -v cnta=$cnta '$2==cnta {print $0}' infile 
 
instead of 
 
awk -v var="$cnta" 'substr($0,8,2)==var {print}' <infile 

I tried the first suggestion, and it too did not quite separate the records. But the 2nd suggestion (thanks Franklin52) seemed to do the trick. I also fixed output filename to make the names easier to read (instead of _1 thru _15 I now create _01 thru _15)
Final code=

cnt=1
max=15
while [ $cnt -le $max ]
   do
   cnta=$(printf "%-2s" $cnt)
   cntt=$(printf "%.2d" $cnt)
   echo $cnta

#   awk 'substr($0,8,2)=="$cnta" {print}' <infile >outfile_${cnt}
#   awk -v cnta=$cnta 'substr($0,8,2)=="$cnta" {print}' <infile >outfile_${cnt}
   awk -v var="$cnta" 'substr($0,8,2)==var {print}' <infile >outfile_${cntt}
   cnt=$((cnt+1))
done

Finally, a look at the output shows that the records were put into the correct output files:

> grep "*" outfile*
outfile_01:12345  1 John Smith   *
outfile_01:50505  1 Wanda Round  *
outfile_02:109    2 Joe Doe      *
outfile_05:14-12  5 Mary Will    *
outfile_13:1-9089 13Harry Can    *
outfile_13:43-4-5 13Kerry Lay    *

which displays filename: and data records (remember, I placed a * at the end of every data record)

Using egrep

egrep "^.{7}$cnta" infile > outfile_${cntt}