Visit Our UNIX and Linux User Community


Incrementing ascii values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Incrementing ascii values
# 1  
Old 02-17-2011
Question Incrementing ascii values

Hi All,

I require some help with the below:

I am trying to incriment the ascii value of a letter and then print it.

So basically "a" becomes "b" and "z" becomes "A".

Does anyone have any pointers?

Cheers,

Parks
# 2  
Old 02-17-2011
Like this?
Code:
echo This is Something z write home aboutZ | tr 'a-zA-Z' 'b-zA-Za'
Uijt jt Tpnfuijoh A xsjuf ipnf bcpvua

Andrew
# 3  
Old 02-17-2011
Code:
#include <stdio.h>

int main(int argc, char **argv ) {
  if ( *argv[1] == 'z' ) *argv[1]=64;
  printf( "%c\n", *argv[1]+1 );
  return 0;
}

Code:
$ cc -o mytest mypoorc.c

$ ./mytest a
b

$ ./mytest o
p

$ ./mytest z
A

This User Gave Thanks to Scott For This Post:
# 4  
Old 02-17-2011
Quote:
Originally Posted by bParks
... incriment the ascii value of a letter and then print it.

So basically "a" becomes "b" and "z" becomes "A".

...

Code:
$
$
$ echo "abcdefghijklmnopqrstuvwxyz" | perl -plne 'tr/a-yz/b-zA/'
bcdefghijklmnopqrstuvwxyzA
$
$

tyler_durden
# 5  
Old 02-17-2011
Quote:
Originally Posted by apmcd47
Like this?
Code:
echo This is Something z write home aboutZ | tr 'a-zA-Z' 'b-zA-Za'
Uijt jt Tpnfuijoh A xsjuf ipnf bcpvua

Andrew
Depending on the locale settings, this may or may not do what you expect. For example, in an en_US.UTF-8 locale, a-z expands to aBbCc...YyZz. a-z and A-Z are almost identical in such a locale; a-z covers all letters except for "A", and A-Z covers all letters except for "z".

It's a good idea to explicitly set the applicable locale environment variables in the script when using range expressions.

Regards,
Alister

P.S. While helping to troubleshoot a script a while back, this particular "feature" claimed an hour of my life. NEVER AGAIN! Smilie

Last edited by alister; 02-17-2011 at 06:02 PM..
# 6  
Old 02-17-2011
Sorry all I should have been more specific,

The aim is to be able to shift the letters via an inputed variable not just one possition.

Unfortunatly c is out of the question so ksh is the form that i require the code to be in.

Thanks for you assistance,
# 7  
Old 02-17-2011
Code:
echo 'a' | nawk -f parks.awk 
echo 'a' | nawk -v shift=4 -f parks.awk

parks.awk:
Code:
function initascii2dec()
{
  for(i=0;i<=255;i++) {
    ch=sprintf("%c", i)
    ascii2dec[ch] = i
  }
}
BEGIN {
  initascii2dec()
  if (!shift) shift=1
}
{
  printf("%c\n", ascii2dec[$1]+shift)
}


Last edited by vgersh99; 02-17-2011 at 08:37 PM..
This User Gave Thanks to vgersh99 For This Post:

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

Featured Tech Videos