473,405 Members | 2,176 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,405 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 3518
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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.