473,378 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

replacing characters is string

hi i would like to know how to replace the 2nd/3rd... character in a string without knowing what value it holds.
Mar 20 '10 #1

✓ answered by rski

get the first character of the string (we want to replace 2nd and 3rd character so the 1st character must rewrite)
Expand|Select|Wrap|Line Numbers
  1. expr substr string 1 1
  2.  

write 'TR' as 2nd and 3rd characters
Expand|Select|Wrap|Line Numbers
  1. echo -n TR
  2.  
write the rest of the string (character from 4th ). if we don't know what is the the lenght of the string we can use length as here.
Expand|Select|Wrap|Line Numbers
  1. expr substr string 4 `expr length\ string`
  2.  
` ` means treat the string inside as a command and execute it.

I advise you to read manual for exec command

10 3516
rski
700 Expert 512MB
You did not give to much info. Can you use sed or similiar tools?
Mar 20 '10 #2
sorry...i canot use sed or awk..
for example : if i do
$ expr index "string" n
i'll get 5, as n is the fith char, so how do i replace the 5th char?
Mar 20 '10 #3
rski
700 Expert 512MB
So you can use only expr?
Mar 20 '10 #4
i can use anything..the only 2 thing i cant use is sed and awk
Mar 20 '10 #5
rski
700 Expert 512MB
You mean something like that?

Expand|Select|Wrap|Line Numbers
  1. echo -n `expr substr string 1 1`;echo -n TR; expr substr string 4 `expr length\ string`
  2.  
it replaces 'tr' to 'TR' in 'string' word.
Mar 21 '10 #6
when u put ; does it mean | can u give an example? i dont really understand the code..im new to unix :)
Mar 21 '10 #7
i dont want to specify the char i want to replace, the the location of the char that i want changed
Mar 21 '10 #8
rski
700 Expert 512MB
I know I do not specify the char i replace but positions of them.
In my resolution I
1) print first character
2) print next two character want to appear in the string
3) print the last part of the string
Mar 21 '10 #9
i see, so what is the 1 1 in your code? and does it have to be a ` ` there? what does it do
Mar 21 '10 #10
rski
700 Expert 512MB
get the first character of the string (we want to replace 2nd and 3rd character so the 1st character must rewrite)
Expand|Select|Wrap|Line Numbers
  1. expr substr string 1 1
  2.  

write 'TR' as 2nd and 3rd characters
Expand|Select|Wrap|Line Numbers
  1. echo -n TR
  2.  
write the rest of the string (character from 4th ). if we don't know what is the the lenght of the string we can use length as here.
Expand|Select|Wrap|Line Numbers
  1. expr substr string 4 `expr length\ string`
  2.  
` ` means treat the string inside as a command and execute it.

I advise you to read manual for exec command
Mar 21 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Xah Lee | last post by:
i have a bunch of java files that has spaced-out formatting that i want to get rid of. I want to replace two end of line characters by one end of line characters. The files in question is unix, and...
5
by: Jim Lawton | last post by:
Hello group, I'm not very experienced in C#, but I can't find any example of this (common) programming problem. I have a string which contains a repeated substring - lets say it's like :- ...
7
by: VMI | last post by:
If I have the string "Héllo", how can I replace char (é) with an 'e'? I cannot use the String.Replace() fuction. It has to be by replacing one char with another. Thanks.
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
0
by: leeonions | last post by:
Hi there, i am trying to use regular expressions to search through a text string and replace a given whole word. take the string = "The matsat on the mat!" (bad example i know) i want to...
2
by: leeonions | last post by:
Hi there, i am trying to use regular expressions to search through a text string and replace a given whole word. take the string = "The matsat on the mat!" (bad example i know) i want to...
2
by: Alain | last post by:
Hi, I am working on a project where I need to convert international characters with acii values. Like André -> andre and Björn -> bjorn. How can I do this without replacing every single...
4
by: Trev | last post by:
Hi everyone, I'm having some problems using Regex; I have a long string that is delimetered in a random fashion by a combination of spaces and \n's for newlines. I have five possiblities: //...
2
by: gsuns82 | last post by:
Hi all, I have to replace accented characters from a input string with normal plain text.I have coded as follows. String input = "ÄÀÁÂÃ"; input=...
7
by: DarthBob88 | last post by:
I have to go through a file and replace any occurrences of a given string with the desired string, like replacing "bug" with "feature". This is made more complicated by the fact that I have to do...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.