Awk - Working with fixed length files

OK I am somewhat new to UNIX programming please see what you can do to help.

I have a flat file that is a fixed length file containing different records based on the 1st character of each line. The 1st number at the beginning of the line is the record number, in this case it's record #1.

I need a slick way to change what's in columns 375-389 (currently: 200607211838001) with the 8 byte date, followed by the julian date followed by a 4 digit incremented sequence number (date +%Y%m%d%j$$$$) example: 200805201410001

Before:
1............................200607211838001..............................
2..................................................................................
1............................200607211838001..............................
2..................................................................................
1............................200607211838001..............................
2..................................................................................
1............................200607211838001..............................

After:
1............................200805201410001..............................
2..................................................................................
1............................200805201410002..............................
2..................................................................................
1............................200805201410003..............................
2..................................................................................
1............................200805201410004..............................

I tried this and this does not work because it's stomping over itself in the loop and setting everything to the last incremented value:

z=0
cat ${file}|grep ^1|while read n
do
z=$(( ${z} + 1 ))
z=`awk 'BEGIN {printf("%0.4d\n", '${z}' )}'`
trans=`date +%Y%m%d%j${z}`
awk '{x=1;if (substr($0,1,1)==x ) print substr($0,1,374) "'"${trans}"'" substr($0,390)} {if (substr($0,1,1)!=x ) print }' ${1} > ${1}.new;mv ${1}.new ${1}
done

Again any help would be greatly appreciated.

If you have a recent version of gawk give the following a try:

BEGIN  {
        FIELDWIDTHS = "29 11 4 500"
        timestamp = strftime("%Y%m%d%j", systime())
}

$1 ~ /^1.+/ {
        seq++
        printf "%s%s%05d%s\n", $1, timestamp, seq, $4
}
$1 !~ /^1.+/ {print}

Of course, the value of FIELDWIDTH needs to be ajusted to your actual record length. But that's the idea.

This does seem to work and thanks! One small issue. I moved this over to another box that is AIX and it bombs out.. Gets this:

awk: 0602-553 Function strftime is not defined.
The source line number is 4.

It doesn't like the strftime function.

Any thoughts??

please excuse me I got it to work! Thanks for all your help!