How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional):

       12345650-000005000GL140227  ANNUAL HELC FEE EN
       22345650-000005000GL140227  ANNUAL HELC FEE EN
       32345650-000005000GL140227  ANNUAL HELC FEE EN

I want to read through the file line by line, assign sh variables to certain substrings, and then echo those values to an output file.

My script looks like this:

while read
do
VAR1=`awk '{print substr($0, 8, 6)}'`
VAR2=`awk '{print substr($0, 14, 2)}'`
VAR3=`awk '{print substr($0, 22, 4)}'`
echo XXX,ABCD,$VAR1,$VAR2,ZZ,$VAR3,HI,EX>>outfile1
done <INPUT_FILE

But my output looks like this:

XXX,ABCD,123456 223456 323456,,ZZ,,HI,EX

I got one line of output. All of the values for $VAR1 were printed, while $VAR2 and $VAR3 were skipped. Instead I want one line of output per line of input. Thank you in advance.

It is not clear what you really want.
But use awk only. awk -F '[ -]' '{awk code here}' infile gives you a way to define field separators, in the example space and hyphen.

example:

awk -F '[ -]'  '{ print $2, $4, $5 }' infile

Let's start from this. Play with it. If you need more help: Please show us your desired output.

Thanks for your reply. What I want is an output file that looks like this:

XXX,ABCD,123456,50,ZZ,5000,HI,EX
XXX,ABCD,223456,50,ZZ,5000,HI,EX
XXX,ABCD,323456,50,ZZ,5000,HI,EX

Instead I get an output file with one line.

Thanks!

awk '{print "XXX","ABCD",substr($0,8,6),substr($0,14,2),"ZZ",substr($0,22,4),"HI","EX"}' OFS=, file

You can also do this in straight bash:

#!/bin/bash
while read
do
    echo XXX,ABCD,${REPLY:7:6},${REPLY:13:2},ZZ,${REPLY:21:4},HI,EX
done < INPUT_FILE > outfile1

Thanks, Jedi Master and Chubler_XL. Both answers worked.

The reason why you are getting one single output line only is that after you redirect stdin to INPUT_FILE, read eats up the first line, awk No. one consumes all the other lines of the file until EOF, and so just one line is printed during the first (and only) loop iteration. And I guess, that "123456" did not really appear in you sample output unless your sample input had an extra line at the top.