Replacing one Char in a string of variable length

Hi all,

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 am using Redhat Ent 4 with ksh

Thanks in advance

Nigel...

I am sorry but I am not able to understand what you want.

what have you tried??
use awk..

Anchal_khare,

Sorry I'll explain a little better.

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.

# X=3;
# echo "NNNCCCXXXX" | sed "s/N/B/$X"
NNBCCCXXXX

That would change the 3rd N to a B in the string not necessarily the 3rd character to a B. Not sure what you want exactly but you might try:

# X=3
# echo "NNNCCCXXXX" | sed "s/./B/$X"
NNBCCCXXXX

What have you tried so far?
Post that code first.

Post atleast 10 lines of input and the expected output.
The more info you give the shorter the program you get.

My code converts this:

012341231231:1:102939283:XYZ:
012341231231:2:102939283:GGGGG:
012341231231:3:102939283:--------:

Into this:

012341231231:1:102939283:BYZ:
012341231231:2:102939283:GBGGG:
012341231231:3:102939283:--B----:

Is this what you want?

danmero,

Your code worked perfectly. Thank you.

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)

while read EachLine
do
SFPSBITS=$(echo "$EachLine" |cut -d":" -f2)
SFRECNO=$(echo "$EachLine" |cut -d":" -f3)
SFPHONESTAT=$(echo "$EachLine" |cut -d":" -f4)
echo "PHONESTAT:$SFPHONESTAT"

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"

# `set_field $LISTNO PHONESTAT PSBITS -f $SFRECNO -l $SFRECNO`
done < file4.1.$$

Just making sure that people dont come here with there home work and also that they do really try to resolve.

OIFS=$IFS
IFS=$':'
position=2; xreplwith="B"
# while read str; do
   str='012341231231:2:102939283:NNN:'
   set $xreplwith $position $(echo "$str")
   echo $3:$4:$5:$(echo $6 | sed "s/./$1/$2"):
# done < file
IFS=$OIFS

Please do the following:

  1. May be the hard coded values position=2; xreplwith="B" should be replace with params sent to the script as $1 and $2.
  2. Remove the "str=" value.
  3. Change the "while read" part as required.