replace character from string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace character from string
# 1  
Old 02-25-2011
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?
# 2  
Old 02-25-2011
omaral,

If sed is an option, you can try with:
Code:
$ echo "------" | sed 's/\(--\)\(-\)\(.*\)/\1X\3/'
--X---

# 3  
Old 02-26-2011
Code:
 $ echo "-----" | ruby -e 's=gets; s[2]="c"; print s' 
--c--

# 4  
Old 02-26-2011
Quote:
Originally Posted by omaral
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?
Code:
awk -v s=$input -v p=$1 '{$p=s}1' FS= OFS= hashfile

# 5  
Old 02-26-2011
Code:
echo "------" |sed 's/-/X/3'

# 6  
Old 02-28-2011
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
# 7  
Old 02-28-2011
Quote:
Originally Posted by cgkmal
omaral,

If sed is an option, you can try with:
Code:
$ echo "------" | sed 's/\(--\)\(-\)\(.*\)/\1X\3/'
--X---

Shorter :
This will replace the 3rd char of the line by a X
Code:
echo '-------' | sed 's/\(..\).\(.*\)/\1X\2/'

Some sed will also support without the \2 notation
Code:
sed 's/^\(..\)./\1X/' infile

This will replace the 3rd '-' wherever it is on the line:
Code:
sed 's/-/X/3' infile

Code:
# 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:

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

example :

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


Last edited by ctsgnb; 02-28-2011 at 10:51 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to replace with a special character in String

Hi, I am beginner to Shell Scripting. I have a String like this "testabcdef", i need the first character as it is and the remaining character should be replaced by the the '*' character. e.g(t***********) PLZ Suggest me. (5 Replies)
Discussion started by: nanthagopal
5 Replies

2. Shell Programming and Scripting

replace a character in a string-help please

I have a string eg. word=promise and I have the masked version of the word which is -------(7dash for each character) if the user input a character O, I want the masked version of the word to be --o---- if the user inputs another character p, then the masked word becomes p-o---- How can i... (1 Reply)
Discussion started by: rajugurung
1 Replies

3. Shell Programming and Scripting

Replace character in certain position in a string

Hello everyone this is my first post of many to come :) I am writing a script and in this script at one point i need to replace a character in a particular position in a string for example: in the string "mystery" i would need to replace the 3rd position to an "r" so the string becomes... (3 Replies)
Discussion started by: snipaa
3 Replies

4. Programming

Question how to replace last character string

Hi, Could someone help me how to replace last character string. For example $>export T1=abcde $>export T2=xyz my question is how to get result abcdxyz? Many Thanks (2 Replies)
Discussion started by: nicklau81
2 Replies

5. Linux

How to replace the 2nd character in a string using sed?

Hi, i have string var1=NN. Based on conditions, i have to change this first N to Y or second N to Y and send the details to other process. How to do that? This is a linux machine. Thanks, Selva (1 Reply)
Discussion started by: bharathappriyan
1 Replies

6. Shell Programming and Scripting

Replace any control character in the string

Need to replace any control character in the string in perl ---------- Post updated at 04:22 PM ---------- Previous update was at 03:50 PM ---------- Any help !!! Thanks in advance (2 Replies)
Discussion started by: hansini
2 Replies

7. UNIX for Dummies Questions & Answers

Find and replace character in a string

Hi all, My problem is the following: I've a script that must list all files in a directory and write this information in a text file. I've tried to get the list through ls command and then write it using msgecho msgecho "`ls $PATH_APS_JOB_ORA`" This works good but the created string... (7 Replies)
Discussion started by: callimaco0082
7 Replies

8. Shell Programming and Scripting

replace character in a string pattern and save the change in same file

I am facing one problem, can any one please suggest me the command for the same in unix. I am using Ksh. I have a large file with the data that looks like below. "ROTO2-2007f","_US01","9/15/2007","9/21/2007",346492,"NICK, LCD WATCH"97,1,"NAPOLITJ ","BERGER,M Z & CO INC",0.01, ... (2 Replies)
Discussion started by: mihir0011
2 Replies

9. Shell Programming and Scripting

replace first character of string sed

I want to change the first/or any character of a string to upper-case: String: test Desired results: Test or tEst or teSt or tesT thanks (6 Replies)
Discussion started by: prkfriryce
6 Replies
Login or Register to Ask a Question