append each line on fixed position 31 to 33

I have a .DAT file like below.

26666666660001343 000001004OLF 029100020090820
27777777770000060 000001004ODL-CH001000020090820
28888888880000780 000001013OLF 006500020090820

.......
........
and so on.....

I want to append each line in a file in .KSH script with XXX with position starting from 31 to 33 in the above file the first record on the position 31-33 empty space is there.. i want to replace with XXX only on this position.

i want the result should be like this..

26666666660001343 000001004OLFXXX029100020090820
27777777770000060 000001004ODL-CH001000020090820
28888888880000780 000001013OLFXXX006500020090820

Can anyone please send me the .KSH script to do this..

thanks

awk 'BEGIN{FS=OFS=""}$31==" "{$31=$32=$33="X"}1' file

Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.

I would try something with cut -d' ' -f3 so that when there is no space there isn't no third field too.
so test the third field and if not empty add 'XXX' at the beginning of that field. Concatenate with the fisrt two fields and it's done.
I can give you an idea of scripting that in bash if you want.
oops ! maybe danmero has found something better with awk !

Well i just used nawk but the outfile remain the same no change..

nawk 'BEGIN{FS=OFS=""}$31==" "{$31=$32=$33="X"}1' Infile.txt > friday.txt

I am using KSH.

Try this

$cat insertxxx.sh

#!/bin/ksh

myFile="${1}"
myOutput="${2}"

awk '{foo=substr($0,31,1); if(foo == "\ ") {$2=$2"XXX"; print $1 "\ " $2 $3;} else print $0;}' "${myFile}" > "${myOutput}"

Thanks all
i used nawk and it works...