change each letter of a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting change each letter of a string
# 1  
Old 06-14-2011
change each letter of a string

Is there a way to change each letter of a string to the next one in the alphabet, so that a becomes b and f becomes g, and digits become one unit bigger - 4 becomes 5 and 9 becomes 0.

I want to change strings like ben123 to cfo234.
# 2  
Old 06-14-2011
I'll assume that the edge case you want 9 to go to 0 rather than 10 or ":"
Code:
 perl -e ' $string="ben123";$string=~s/9/0/g;$string=~s/([^9])/chr(ord($1) +1)/eg;print "$string\n";

# 3  
Old 06-14-2011
Try with tr command.
Code:
tr 'abc123' bcd234' < inutfile #add remaining alphabets and numbers

# 4  
Old 06-14-2011
Using hexdump:
Code:
hexdump -e '/1 "(%u+1)%%256\n"' file | bc | awk '{printf("%c", $0)}'


Using only POSIX tools and features:
Code:
od -An -tu1 file | tr -c '[0123456789]' '[\n*]' | sed '/..*/s//(&+1)%256/' | bc | awk '{printf("%c", $0)}'

Note: Some hexdump implementations have (or used to have) problems with the %% sequence. Instead of printing a literal percentage symbol as expected, they complain about a bad conversion character.

Regards,
Alister
# 5  
Old 06-14-2011
You can do it all in one tr, no need to od | tr | awk | sed | this | that | whatever. Just match your output set to what you want.

input: a-z
output b-za

input: A-Z
output: B-ZA

input: 0-9
output: 1-90

Code:
$ echo ben123 | tr '[a-zA-Z0-9]' '[b-zaB-ZA1-90]'
cfo234
$

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 06-14-2011
Thank you, I was looking for something like the last one.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue catching/reading 2 digits and 3 letter string between brackets

Hello I'm writing a handler for ffmpeg, and having troubles to catch some exceptions that may occour with certain files. In order to parse for video & subtitle maps, i've had to make the raw data easier to handle, until now this worked well, but basicly i've just been lucky... The input... (1 Reply)
Discussion started by: sea
1 Replies

2. Shell Programming and Scripting

Counting all words that start with a capital letter in a string using python dictionary

Hi, I have written the following python snippet to store the capital letter starting words into a dictionary as key and no of its appearances as a value in this dictionary against the key. #!/usr/bin/env python import sys import re hash = {} # initialize an empty dictinonary for line in... (1 Reply)
Discussion started by: royalibrahim
1 Replies

3. Shell Programming and Scripting

Change first letter of a word from lower case to upper case

Hi all, I am trying to find a way to change first letter in a word from lower case to upper case. It should be done for each first word in text or in paragraph, and also for each word after punctuation like . ; : ! ?I found the following command sed -i 's/\s*./\U&\E/g' $@ filenamebut... (7 Replies)
Discussion started by: georgi58
7 Replies

4. Shell Programming and Scripting

check index of a string by finding a letter in it

i would like to search for a letter in a string and get its index position. example: name='john' pos=$(expr index $name o) the result will be equal to 2 (2nd position) how do you make this thing not case sensitive? example: name='john' pos=$(expr index $name O) the... (1 Reply)
Discussion started by: kokoro
1 Replies

5. Shell Programming and Scripting

Trying to take a string and break each letter into a separate variable

I am trying to make a script that takes a word and each letter up and turns it into a separate variable. My code currently does not work but I feel I just need to tweak one thing that I am unsure of. (ex: if forum was typed in letter1=f; letter2=o; letter3=r;...) Thank you count=1; ... (7 Replies)
Discussion started by: crimputt
7 Replies

6. Shell Programming and Scripting

HOWTO - change letter case of variable value

Hi I have e.g. VAR1=january and I need to change it into VAR1=January. How to change value of VAR1 variable to always set first character uppercase and other lowercase ? thx for help. (9 Replies)
Discussion started by: presul
9 Replies

7. Shell Programming and Scripting

First Letter in a String to be capitalized

Is there a way to cnvert first letter alone in a string to upper case. For eg: diamond should be converted to Diamond. Thanks in Advance, Kinny (6 Replies)
Discussion started by: kinny
6 Replies

8. Shell Programming and Scripting

Extract the letter from string

Hi, Pl help me. User is entering the values for the given input Enter keys: secret1 secret2 secret3 can i extract the last digit of each values in the keys.. here for ex. 1 2 3 can we get this using shell script? (22 Replies)
Discussion started by: balamv
22 Replies

9. Shell Programming and Scripting

Finding a letter in a string

How to check whether a particular string contains dot or not? Here I can not use grep as the string is not in a file. I will get this string from user input Thanks, (2 Replies)
Discussion started by: balamv
2 Replies

10. Shell Programming and Scripting

get only two letter from any string

I want get middle two latter of any string. Input: var="070108" output: var1="01" please help. (2 Replies)
Discussion started by: rinku
2 Replies
Login or Register to Ask a Question