Replace character in certain position in a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace character in certain position in a string
# 1  
Old 04-25-2011
Replace character in certain position in a string

Hello everyone this is my first post of many to come Smilie

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 "myrtery"

i cant use cut because the loop needs to update the string on several occations on different positions so for example next time over i need to replace the 4th position to "y" the string becomes "mirytery"

any ideas as to how I can do this?? any help is greatly appreciated
# 2  
Old 04-25-2011
See if this can help you:
Code:
#!/usr/bin/ksh
typeset -i mC=1
mStr="abcdefghij"
while [[ ${mC} -le 10 ]]
do
   echo ${mStr} | sed "s/./M/${mC}"
   mC=${mC}+1
done

# 3  
Old 04-25-2011
What about something like?

Code:
$ pos=3

$ repl=r

$ echo mystery | awk -v pos=$pos -v repl=$repl '{print substr($0,1,pos-1) repl substr($0,pos+1)}'
myrtery

# 4  
Old 04-25-2011
Code:
$ printf "mystery" | ruby -e 's=gets; s[2]="r";puts s'
myrtery

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

2. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

3. Emergency UNIX and Linux Support

Replace nth position character of all the lines in file

I want to replace 150th character of all the lines in a file using sed or awk... searched the forums but didn't find exact answer (9 Replies)
Discussion started by: greenworld123
9 Replies

4. Shell Programming and Scripting

How to extract the certain position's character in a string

Suppose there are two files: A, format is like: line1 12 line2 33 line3 6 ... B, format is like: >header taaccctaaccctaaccctaacccaaccccaccccaaccccaaccccaac ccaaccctaaccctaaccctaacccaaccctaaccctaaccctaacccaa ccctcaccctcaccctcaccctcaccctcaccctcaccctcaccctaacc... (1 Reply)
Discussion started by: bioinflix
1 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

7. UNIX for Dummies Questions & Answers

How to replace character on defined position

I need to replace the character on 6th position. If this character is 1 I have to repleace it with A, if it is 2 than I have to replace it with B. If it is not 1 or 2 I should not repleace it. input: abcd defg abcd 1efg mnop weac rstu 2bcd i need: abcd defg abcd Aefg mnop weac rstu... (2 Replies)
Discussion started by: necroman08
2 Replies

8. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

9. Shell Programming and Scripting

Search for a string and replace the searched string in the same position

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last... (15 Replies)
Discussion started by: ganesh_248
15 Replies

10. Shell Programming and Scripting

How to add character in specific position of a string?

Hi All, I would like to use sed to add "-" between the following string: Value: 20060830 Result: 2006-08-30 Pls advice. Thx a lot Victor (5 Replies)
Discussion started by: victorlung
5 Replies
Login or Register to Ask a Question