The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Can i read a file character by character karnan Shell Programming and Scripting 6 05-18-2008 11:22 PM
bash while read how to remove \n character papasj Shell Programming and Scripting 2 02-24-2008 08:01 AM
Read First Character of Each Line in File azelinsk Shell Programming and Scripting 7 07-11-2007 05:15 PM
Can I read a file character by character? murtaza Shell Programming and Scripting 3 03-29-2007 08:19 AM
need to read 3° character from a text file piltrafa UNIX for Dummies Questions & Answers 15 07-26-2005 07:19 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-06-2008
Registered User
 

Join Date: Jun 2008
Posts: 1
Stumble this Post!
read a variable character by character, substitute characters with something else

im having trouble doing this:

i have a variable with 2 characters repeating e.g. aababbbaaaababaabbaabbba

is there a way i can search the variable for a's and b's and then change a's to b's and b's to a's?

im guessing its like getting the 1's compliment of the string

im doing this in BASH, and ive looked into seeing if sed or awk could help but i got nothing

any help would be greatly appreciated
thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-06-2008
Registered User
 

Join Date: Apr 2008
Location: European Union/Germany
Posts: 183
Stumble this Post!
echo $var | tr a z | tr b a | tr z b

more info: man tr
we use z as temporary placeholder as we can only transform one character a time
Reply With Quote
  #3 (permalink)  
Old 06-06-2008
joeyg's Avatar
premier etoile de match
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 625
Stumble this Post!
Wink I think you may need to use a 'dummy' character

In order to switch things, the first cannot immediately be switched to the second. Take a look at the following where I ...
translate a to z
translate b to a
translate z to b

Code:
> echo $var1
aababbbaaaababaabbaabbba
> var2=$(echo $var1 | tr a z)
> var3=$(echo $var2 | tr b a)
> var4=$(echo $var3 | tr z b)
> echo $var4
bbabaaabbbbababbaabbaaab
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:28 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0