Incrementing ascii values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Incrementing ascii values
# 8  
Old 02-17-2011
Thanks vgersh99 thats what I was after!
# 9  
Old 02-17-2011
shell, without external commands:
Code:
asciinext()
{
  printf "\\$(printf "%03o" $(( $(printf "%d" \'$1 ) + 1 )) )\n" 
}

Code:
$ asciinext a
b


Last edited by Scrutinizer; 02-17-2011 at 09:32 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 10  
Old 02-17-2011
Code:
 $ echo "a" | ruby -e 'puts gets.succ' 
b

# 11  
Old 02-17-2011
Here is a solution in pure ksh that wraps properly, Scrutinizer was on the right track and I think if his solution worked (eg Z -> a and z -> a dont work), it would have been better than this.

Code:
#!/bin/ksh
A=( A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    a b c d e f g h i j k l m n o p q r s t u v w x y z )
INC=$1
CNT=${#A[@]}
SU=$(printf "%d" \'A)
EU=$(printf "%d" \'Z)
SL=$(printf "%d" \'a)
EL=$(printf "%d" \'z)
shift
echo $@ | while read -n 1 L
do
   i=0
   p=$(printf "%d" \'$L)
   n=99
   [ $p -ge $SU -a $p -le $EU ] && n=$(( (p-SU+INC)%CNT))
   [ $p -ge $SL -a $p -le $EL ] && n=$(( (26+p-SL+INC)%CNT))
   [ $n -eq 99 ] && printf "$L" || printf "%s" ${A[n]}
done
echo

Code:
$ ./shift.ksh 1 Zhis is z test
aijt jt A uftu
 
$ ./shift.ksh 26 Zhis is z test
zHIS IS Z TEST
 
$ ./shift.ksh -2 $(./shift.ksh 2 This is a test)
This is a test


Last edited by Chubler_XL; 02-18-2011 at 12:16 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Hex to Ascii in a Ascii file

Hi All, I have an ascii file in which few columns are having hex values which i need to convert into ascii. Kindly suggest me what command can be used in unix shell scripting? Thanks in Advance (2 Replies)
Discussion started by: HemaV
2 Replies

2. Shell Programming and Scripting

Replace alphabets in a string with their ASCII values

hi. I have a requirement where I need to REPLACE all alphabets from an alphanumeric input string into their respective ASCII decimal value. For example: If the input string is ABAC123, the output should be 65666567123 I am seeking a single line command, and I was trying searching for options... (21 Replies)
Discussion started by: kumarjt
21 Replies

3. UNIX for Beginners Questions & Answers

Convert ascii character values to number that comes between the numbers in UNIX

I have variable that contains multiple values of number and also include overpunch(i.e. # $ % etc) character so we want to replace it with numbers. here are the example: Code: 11500#.0# 28575$.5$ 527#.7# 42".2" 2794 .4 2279!.9! 1067&.7& 926#.6# 2279!.9! 885".5" 11714$.4$ 27361'.1'... (1 Reply)
Discussion started by: nadeemrafikhan
1 Replies

4. Shell Programming and Scripting

Anagram finder based in Ascii values

Hello, i need some help with a programm i want to make, what i want to do is to make a dictionary and include some anagrams with it, and make the programm read the Ascii value of each word, and compare them with the anagrams and make the programm print the words that have the same Ascii value,... (1 Reply)
Discussion started by: jose2802
1 Replies

5. Shell Programming and Scripting

Auto incrementing a particular column values

Hi All, I have the input file(.csv) like below. SNo,Step,Data 1,0,1 2,0,2 3,1,1 4,2,2 5,0,3 6,0,4 and I need the following output. 1,0,1 2,0,2 3,1,1 4,2,2 5,3,3 6,3,4 (5 Replies)
Discussion started by: ks_reddy
5 Replies

6. Shell Programming and Scripting

Unix c-shell - replacing/incrementing values in columns?

2 21 1 12 3 123 4 1234 6 49 0 49 33 212 I need to replace/increment all the values in the 2nd column that correspond to 0 in the first column. so for 0 49 i would get 0 50 this can be done through: paste num4.txt... (14 Replies)
Discussion started by: audrey_flox
14 Replies

7. Shell Programming and Scripting

convert ascii values into ascii characters

Hi gurus, I have a file in unix with ascii values. I need to convert all the ascii values in the file to ascii characters. File contains nearly 20000 records with ascii values. (10 Replies)
Discussion started by: sandeeppvk
10 Replies

8. Post Here to Contact Site Administrators and Moderators

No. post not incrementing

Hi Admin, i just noticed that when I do postings, the number does not increment. eg : Post A -Total Posts 312 Post B - Total Posts 312 Post C - Total Posts 313 Post D - Total Posts 313 Why is this so? Can you kindly check this out? Thank you. (5 Replies)
Discussion started by: incredible
5 Replies

9. UNIX for Advanced & Expert Users

Converting Binary decimal coded values to Ascii Values

Hi All, Is there any command which can convert binary decimal coded values to ascii values... i have bcd values like below оооооооооооо0о-- -v - Pls suggest a way to convert this. Thanks, Deepti.Gaur (3 Replies)
Discussion started by: gaur.deepti
3 Replies

10. UNIX for Dummies Questions & Answers

grep using ASCII values

machine: HPUX file: a.dat contents: decimal 1 decimal 2 string 1 string 2 ASCII value of 'd': 100. to grep lines that have 'd', I use the following command grep d a.dat My requirement: I should grep for lines that contain 'd'. But I should use ASCII value of 'd' in the command... (1 Reply)
Discussion started by: sriksama
1 Replies
Login or Register to Ask a Question