Register
It is currently Tue Sep 30, 2014 5:47 am

Modifying a character in a string variable


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Fri Mar 12, 2010 11:41 am   

Joined: Tue Mar 02, 2010 9:55 pm
Posts: 24
Location: Canada
Does anyone know how to do this...Can it be done?

I can extract the single characters and print then, I can substitute a string of characters in a string variable while echo'ing it...but I can't find a way to modify a string character by character so that the variable is changed...Does anyone know how to do this? Here's what I have so far...

Code:
#! /bin/sh

echo -n "enter a string->"
read mystr

echo $mystr


strlgth=${#mystr}
cntr="0"

while [ $cntr != $strlgth ]
do   
   echo ${mystr:$cntr:1}
   cntr=`expr $cntr + 1`
done

echo ${mystr/"is"/"are"} #will replace is with are in the echo

echo $mystr #back to original here

exit 0


What I want...

mystr="this is the string"
do something here to change the third character in, say form 'i' to 'g'
so when I echo &mystr it will display 'thgs is the string'

echo $mystr -> will display thgs is the string


Top
 Profile  
 PostPosted: Fri Mar 12, 2010 12:50 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 549
to change the third character from whatever to 'g'
Code:
mystr="this is the string"
myNewStr="${mystr::2}g${mystr:3}"
echo "$myNewStr"
thgs is the string


Top
 Profile  
 PostPosted: Fri Mar 12, 2010 3:22 pm   

Joined: Tue Mar 02, 2010 9:55 pm
Posts: 24
Location: Canada
Thanks for the reply Watael, your solution works but looking at it I'm wondering...can I change just the character in the string without having to build and re-save the entire string again like your solution does.
I can explain what I want better in C.

If I have a character - ch[] - array like below.

char ch[] = "this is the string!\n";

now to change the third character I would

ch[2] = 'g';

now the character array is - "thgs is the string!\n"

I guess what I'm after is, can a bash script change something within a variable without having to re-save the entire variable? Hope this makes sense..


Top
 Profile  
 PostPosted: Fri Mar 12, 2010 4:02 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 549
AFAIK, no.


Top
 Profile  
 PostPosted: Fri Mar 12, 2010 4:41 pm   

Joined: Tue Mar 02, 2010 9:55 pm
Posts: 24
Location: Canada
Again thank-you for your replies...I couldn't find anything googling so wasn't sure if the functionality existed. It doesn't...Thank-you for the clarification...G4143


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP