I have a string
-----
i want to replace the 3rd '-' with a character
How can i do that
Basically im trying to do hangman
I have added the '-----' string to a file called hash
and i have the character in a variable called $input
also i have the character location(index) in this variable $1
How can i go on replacing the $1 character of the hash file with the $input character?
were hash is the file that contains the hash of the word '--------'
$input is the inputed character
$i is were the character should go (index of the character)
but i have one problem which is
if a word like 'kitten' comes up and i input 't' the output is
--t-t-
or internet and i input e
---e---e
the second instance of the same character is always one place ahead
my code
for ((i=1;i!=$charCount;i++)); do
charCut=$(cut randomWordFile -c$i)
if [ $input = $charCut ]; then
cat hash | sed "s/-/$input/$i" 1> hash
fi
$charCount is the character count and is always 1 more. e.g the word 'car' is 4 characters
$input is what the user inputs or it is the gussed character
hash is the file that contians the hash '-----'
randomWordFile is the file that contains the word
---------- Post updated at 03:25 PM ---------- Previous update was at 03:19 PM ----------
Basically
i need to take the $i character of the word and replace it with $input and not take the $i instance of the word and replace it with the character
You can simplify that quite a bit using a posix-standard substitution flag:
sed 's/./X/3'
Regards,
Alister
---------- Post updated at 03:15 PM ---------- Previous update was at 03:12 PM ----------
Woops. I started reading this thread backwards and just now saw that you and others have already covered that. I apologize. In a short while I'll have breakfast before I get out of bed and then wake up.