I know this isn't a good thing to do and/or a good solution but I was just messing around trying to answer this question and I came across the following problem.
The goal is to read the input file character by character and if the character is within the specified range, replace it.
NNNNN NNNNN NNNNN NNNNN
#!/bin/bash #set -x input=~/tmp/in declare -i count=0 low=$1 high=$2 new_char=$3 while IFS=$'\n' read -r line; do while IFS= read -rn1 char; do if ((count>=low)) && ((count<=high)); then printf '%s' "$new_char" else printf '%s' "$char" fi ((count++)) done <<<"$(printf '%s' "$line")" echo done <"$input"
Desired output (when run as:
./script.sh 10 13 P):
NNNNN NNNNP PPPNN NNNNN
$ ./script.sh 10 13 P NNNNN NNNNPP PPNNN NNNNN
I'm not sure why it's seemingly creating a new character out of thin air on the 2nd line and not replacing the 13th character on the 3rd line. I've tried moving the
((count++)) statement to the beginning of the second while loop, I've tried starting at
count=1, I've tried seemingly every combination of
((count+1)). With varying results but all seem to add an extra character on the second line.
I've double checked that the input file has no trailing whitespace on any of the lines.