Using subsring in ksh93 - How to stuff a byte back in

#!/bin/ksh93                 # set to ksh93 so we can use substring 
cat "file" |
while IFS=\n read I          # read the next record into I
do                           # do not parse out blanks, tabs in the record by setting IFS to newline char only
  D1=${I:27:1}               # isolate byte 27 into D1
  if [[ "$D1" = " " ]]       # if byte 27 = a blank
  then
     ${I:27:1}="-"           # change byte 27 to a dash "-"   <-- ### WARNING, this syntax is invalid ###          
  else
    :                        # no change to byte 27 
  fi  
  print "$I"                 # write out record  
done  

A question on the above code:

  • is there a simple way (using substring) to modify byte 27 using the above example ?

FWIW, I wasn't able to come up with any syntax that would allow me to substring the desired change back into the record. I had to use other techniques to accomplish the same thing..... but was hoping the experts here might see something simple, that no doubt I missed. Thx for any help.

You seem to have some mutant form of ksh93. That syntax is not legal. You should be using colons, not commas...

$ x=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLNMOPQRSTUVWXYZ
$ echo $x
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLNMOPQRSTUVWXYZ
$ echo ${x,27,1}
/opt/ksh93/bin/ksh: syntax error: `,' unexpected
$ echo ${x:27:1}
B
$ echo ${.sh.version}
Version M 1993-12-28 m+
$

Would you do an "echo ${.sh.version}" and post the results? Also what system are you using? I hesitate to present my solution since I cannot predict the behavior of your shell, but here it is anyway....

$ x=${x:0:27}'-'${x:28}
$ echo $x
abcdefghijklmnopqrstuvwxyzA-CDEFGHIJKLNMOPQRSTUVWXYZ
$

ooops, yes I screwed up..... you are correct.... the commas should be colons. I'll try to edit my original post & fix that. I wrote my post from memory (not a C&P of my original script) and messed the substring syntax up. Sorry for the confusion this caused.

thx for taking the time to answer.... your solution -->

is simple & very straightforward. Just what I was looking for. Thx again.