I am trying to find the best way of making a change to 1 char in a string, the string can be between 1 and 14 characters.
I am reading a line in from a file which contains
012341231231:2:102939283:NNN: Require :NBN:
012838238232:3:372932:NNN: Require :NNB:
I need to change 1 N or a B depending on the number in the 2nd field (a 2 or a 3 in the cases above).
Now each character in the NNN field could initially be N,C,B, or 2 or 3 other characters.
At the moment I can only think of looping around each character using a cut -c to write out each character as it already is then changing the one I want when I get to it, but I was hoping there could be a better and more efficient way.
I have a string "NNNCCCXXXX" for example. I will need to change one of these characters to B so for example if I needed to change Char 3 I would want to have "NNBCCCXXXX" I will know the char I need to change and the current string.
To answer Vidyadhar85's questions. I have not tried awk, as I have never used it. Just trying to gain best approach then I'll start researching and coding.
Edidataguy, I was looking for the best approach rather than just looking for someone to provide the code, hence why I did not attach any initial code.
I always try to research approch first then code. I was hoping for a reply like, use Sed, or loop through each bit and replace the one you want, then I would have researched that.
I am new to shell, (used to VB) so I was planning to use grep -c to read each char in turn and just write out to a new string until I got to my bit to change etc... This would have taken 6-7 lines of code but sed, did it perfectly.
I now have ordered a sed book to study.
My final code is below. (echo's will be replaced with a -v for production, and -d for write to log eventually)
echo "Bit to change:$SFPSBITS - Record to change:$SFRECNO"
echo "Current PHONESTAT string is $SFPHONESTAT"
SFNEWPHONESTAT=$\(echo "$SFPHONESTAT" | sed "s/./B/$SFPSBITS"\)
echo "New PHONESTAT string:$SFNEWPHONESTAT"