replace character from string

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?

omaral,

If sed is an option, you can try with:

$ echo "------" | sed 's/\(--\)\(-\)\(.*\)/\1X\3/'
--X---
 $ echo "-----" | ruby -e 's=gets; s[2]="c"; print s' 
--c--  
awk -v s=$input -v p=$1 '{$p=s}1' FS= OFS= hashfile
echo "------" |sed 's/-/X/3'

can you please help me
$input is the input character
$i is were the character should go
hash is the file the contains this '-----'

cat hash |sed 's/-/$input/$i'
this didnt work

Shorter :
This will replace the 3rd char of the line by a X

echo '-------' | sed 's/\(..\).\(.*\)/\1X\2/'

Some sed will also support without the \2 notation

sed 's/^\(..\)./\1X/' infile

This will replace the 3rd '-' wherever it is on the line:

sed 's/-/X/3' infile
# echo "--------" | sed 's/-/X/3'
--X-----

---------- Post updated at 03:56 PM ---------- Previous update was at 03:42 PM ----------

to replace the n th character:

sed 's/^\(.\{n-1\}\)./\1X/' infile

example :

$ echo '--------' | sed 's/^\(.\{2\}\)./\1X/'
--X-----

in my case

x is $input
n is $i
infile is hash

To make it "work" you should use double quote instead of single quote, but your formula is NOT correct :
if you have

--X-----

and you want then :

--XY----

Y is at the 4th position, but if you run

echo "--X-----" | sed 's/-/Y/4' 

you will get

--X-Y---

because the sed statement will replace the 4th '-' instead of the 4th character.

I have updated my previous post about how to change the n th character, pls check it

---------- Post updated at 04:10 PM ---------- Previous update was at 04:04 PM ----------

... by the way, using "hash" as a filename is a bad idea since "hash" already is a standard unix command name.

Thank you

This is what i have done

cat hash | sed "s/-/$input/$i" 1> hash

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

to replace the n th character:

sed 's/^\(.\{n-1\}\)./\1X/' infile

example to replace the 3rd character :

 $ echo '--------' | sed 's/^\(.\{2\}\)./\1X/'
--X-----

---------- Post updated at 04:54 PM ---------- Previous update was at 04:29 PM ----------

$ a="----------"
$ input=K
$ i=3
$ echo $a | sed 's|^\(.\{'"$(($i-1))"'\}\).|\1'"${input}"'|'
--K-------
$ input=Z
$ i=5
$ echo $a | sed 's|^\(.\{'"$(($i-1))"'\}\).|\1'"${input}"'|'
----Z-----

Hi, ctsgnb:

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. :wink:

Cheers,
Alister

Hahah yes, how did i miss that one ??? ...lol

I mentionned sed 's/-/X/3' and i didn't even think about changing the - by the dot !!

I guess it is me who need a break ! ... :slight_smile: