I want to replace a specific character "M" in a line of text with either "A" or "T". The choice for whether to replace with "A" or "T" should happen at random for each "M" in the line of text.
I tried to write a script using sed to do this, but the evaluation of the random pick of "A" or "T" happens only once on the whole line, rather than at every replacement. My script looks like this:
#!/bin/bash
ambM[0]=A
ambM[1]=T
file_in=${1?Error: no input file}
cat $file_in | sed "s/M/${ambM[$[$RANDOM % 2]]}/g"
But if I use this with a file that is a single line of "M"s:
MMMM
I'll get either all "A"s
AAAA
Or all "T"s
TTTT
Is there something that can be done to make this work with sed? Or maybe an equivalent way to do this with awk? Thanks for any help!